Calculating distances (across matrices)

October 5, 2012

(This article was first published on is.R(), and kindly contributed to R-bloggers)

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 on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

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.