Calculating distances (across matrices)

October 5, 2012

[This article was first published on is.R(), and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This Gist is mostly for my future self, as a reminder of how to find distances between each row in two different matrices. To create a distance matrix from a single matrix, the function dist(), from the stats package is sufficient.

There are times, however, when I want to see how close each row of a matrix is to another set of observations, and thus I want to find distances between two matrices. For example, consider a set of voter ideal points in several dimensions, from which I want to find the distance to a set of candidate ideal points in those same dimensions.

Creating a distance matrix can get very memory-intensive, so it is useful to focus only on finding the distances one needs, rather than calculating an entire n × n matrix and ignoring most of it. For this purpose, I use the dist() function from the proxy package, as shown below.

I also include an example of the use of multidimensional scaling on a distance matrix, to show how useful this simple operation can be.

To leave a comment for the author, please follow the link and comment on their blog: is.R(). offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , ,

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)