An example of using a matrix to find which cities are closest to one
another. The file distances-from-google.r
downloads some sample data
from Google's Distance Matrix API and converts it from a JSON object to
an R matrix. The file distance-matrix.r
creates a function to find the
closest city in each row of a distance matrix.
Results:
- New York is closest to Philadelphia
- Los Angeles is closest to Houston
- Chicago is closest to Philadelphia
- Houston is closest to Chicago
- Philadelphia is closest to New York
So the relationships are not symmetrical: New York and Philadelphia are closest to one another, but Chicago is closer to Philadelphia even though Houston is closer to Chicago.
One could easily use this function to make a map of closest locations for historical purposes.