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.