Here you will find daily news and tutorials about R, contributed by over 573 bloggers.
There are many ways to follow us - By e-mail:On Facebook: If you are an R blogger yourself you are invited to add your own R content feed to this site (Non-English R bloggers should add themselves- here)

(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.