1

I am using the gmapsdistance package to compute the travel time between two zip codes.

It seems to work semi-well. There are still some errors with zip codes which are nearly identical and sometimes the function does not output the correct time.

The main problem is that it takes hours to run the function on a data set of the size I am working with.

Here is an example data frame of the data set.

library(gmapsdistance)

df <- data.frame(
            "ZIP_START" = c(95051, 94534, 60193, 94591, 94128, 94015, 94553, 10994, 95008), 
            "ZIP_END" = c(98053, 94128, 60666, 73344, 94128, 73344, 94128, "07105", 94128), 
           stringsAsFactors = FALSE)

df$travel_time <- mapply(gmapsdistance, df$ZIP_START, df$ZIP_END, 
                         mode = "driving", key = get.api.key(), 
                         traffic_model = "best_guess")

Is there a way to improve the speed of this function? I tried looking at the mclapply package but it does not seem to available in my R version.

Originally I wrote a for loop but it took over 10 hours to run, so I thought that using mapply method would be faster, but it still is taking a great deal of time.

M--
  • 25,431
  • 8
  • 61
  • 93
mrsquid
  • 605
  • 2
  • 9
  • 24
  • 1
    Might this be related to throttling from the underling google API? https://cran.r-project.org/web/packages/gmapsdistance/readme/README.html https://developers.google.com/maps/documentation/distance-matrix/usage-and-billing – Jon Spring Apr 17 '19 at 22:32
  • I see yeah it might very well be. Would there be any way to improve then? – mrsquid Apr 17 '19 at 22:35
  • 3
    As a start, you might try reducing the number of calls by grouping zips based on their first three or four digits, which might make for many fewer combinations to calculate, at the cost of some accuracy. e.g you might have 95051 to 98053 (16 hour drive), but also 95050 to 98010 (15 hour drive). – Jon Spring Apr 17 '19 at 23:04
  • I see thanks that makes sense I will try that out. – mrsquid Apr 17 '19 at 23:15

0 Answers0