Skip to content

Instantly share code, notes, and snippets.

@lmullen
Last active August 25, 2019 14:22
Show Gist options
  • Save lmullen/9730478 to your computer and use it in GitHub Desktop.
Save lmullen/9730478 to your computer and use it in GitHub Desktop.
Distance matrix in R

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.

# Stored distance matrix from distances-from-google.r so we don't need to use
# the API for demonstration purposes
m <- structure(c(0, 4470438, 1269091, 2620357, 151171, 4490989, 0, 3242896,
2488156, 4362620, 1269991, 3243861, 0, 1742321, 1220580,
2620117, 2488397, 1742043, 0, 2487807, 151690, 4362957,
1220015, 2490898, 0), .Dim = c(5L, 5L), .Dimnames =
list(c("New York, NY, USA", "Los Angeles, CA, USA", "Chicago, IL, USA",
"Houston, TX, USA", "Philadelphia, PA, USA"), c("New York, NY, USA",
"Los Angeles, CA, USA", "Chicago, IL, USA", "Houston, TX, USA",
"Philadelphia, PA, USA")))
# Determine which city is closest in a given row. Returns the column number of
# the closest city.
closest_city <- function(dist_matrix) {
dist_matrix[dist_matrix == 0] <- NA
dist_matrix <- apply(dist_matrix, 1, which.min)
return(dist_matrix)
}
# Human-readable results
results <- closest_city(m)
cbind(rownames(m), colnames(m)[results])
# Get a distance matrix from Google's Distance Matrix API
# https://developers.google.com/maps/documentation/distancematrix/
library(httr)
# Five most populous US cities in 2012
# http://en.wikipedia.org/wiki/List_of_United_States_cities_by_population
cities <- "New+York+NY|Los+Angeles+CA|Chicago+IL|Houston+TX|Philadelphia+PA"
# Request object from API
r <- GET(
"http://maps.googleapis.com/maps/api/distancematrix/json",
query = list(
origins = cities,
destinations = cities,
sensor = "false")
)
stop_for_status(r)
distances <- content(r)
m <- matrix(nrow = 5, ncol = 5)
rownames(m) <- unlist(distances$destination_addresses)
colnames(m) <- unlist(distances$origin_addresses)
# I'm sure there's a more idiomatic way
for (i in 1:length(distances$rows)) {
for (j in 1:length(distances$rows[[i]]$elements)) {
m[i,j] <- distances$rows[[i]]$elements[[j]]$distance$value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment