R Speed up Mapply function which finds travel time between two zip codes

266 Views Asked by At

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.

0

There are 0 best solutions below