2

I've been using the mapdist funtion() for determining the distance between two zipcodes. I am not very good with loops yet and was wondering how to loop in multiple zipcodes so that I don't have to rerun the code every time.

Code posted below.

library(ggmap)
mapdist('95077','06473', mode = 'driving')
adura826
  • 103
  • 1
  • 1
  • 10
  • It's unlikely that you will a loop. You can pass one vector with all the `from` (first argument of `mapdist`) and one vector with all the the `to` (second argument of the function). – Pasqui May 18 '18 at 16:31

1 Answers1

1
library(ggmap)

Building an example data.frame

geoData <- data.frame(FROM = c('95077', 'Manchester Deaf Institute'),
                  TO = c('06473', 'Birmingham O2 Academy 1'), 
                  stringsAsFactors = FALSE) 

passing columns as args

mapdist(from = geoData[['FROM']], 
    to   = geoData[['TO']], 
    mode = 'driving')

result

                       from                      to       m       km      miles seconds   minutes     hours
1                     95077                   06473 4932333 4932.333 3064.95173  161558 2692.6333 44.877222
2 Manchester Deaf Institute Birmingham O2 Academy 1  141330  141.330   87.82246    6569  109.4833  1.824722
Pasqui
  • 591
  • 4
  • 12
  • ggmap is now a paid service, so you can use geosphere as indicated in this post: https://stackoverflow.com/questions/55408526/r-find-the-distance-between-two-us-zipcode-columns – Nikhil Gupta Apr 19 '20 at 21:00