Distance between unequal number of locations in R (latitude and longitude are inputs)

Viewed 171

How do I calculate the distance between each location in one data frame versus each in another when they have a different number of rows?

For example, say I have dataframe A with 3 rows and dataframe B with 4 rows. These are from the SNAP (United States federal food / beverage assistance for low-income households) Retailer Locator :

A
      Store_Name     Longitude Latitude
1     Food Lion 2213 -80.86581 35.59477
2 THE CORNER GROCERY -81.09917 35.26776
3          FISH WING -80.88245 35.21639


B
         Store_Name     Longitude Latitude
1    SUPERIOR GROCERIES -79.80839 35.73597
2     MORVEN TRUCK STOP -80.01122 34.88312
3       GREENHILL STORE -81.99146 35.34768
4 NORTHSIDE FOOD MARKET -77.94242 34.24158

Here are some failed attempts:

mapdist(as.character(a), as.character(b)) </code>

YIELDS: from and to entries that resemble the following and only one distance calculation that is possibly useful: c(35.594765, 35.267761, 35.216393)

distcomp <- mapdist(from = c(lon = as.character(a$Longitude), lat = as.character(a$Latitude)), to = c(lon = as.character(b$Longitude),   lat = as.character(b$Latitude)), mode = "driving")

YIELDS: Error <code> arguments imply differing number of rows: 6, 8

# row-bind the rows even though this would mean extra work so that I could only have the distances from those in <code>a</code> to those in <code>b</code>:

c <- rbind(a,b)
distcomp <- mapdist(from = c(lon = as.character(c$Longitude), lat = as.character(c$Latitude)), 
                     to = c(lon = as.character(c$Longitude),   lat = as.character(c$Latitude)), mode = "driving")

YIELDS: A bunch of NAs in the results. Nothing helpful.

1 Answers
Related