简体   繁体   中英

Using a loop to apply gmapsdistance to a list in R

I am trying to use the gmapsdistance package in R to calculate the journey time by public transport between a list of postcodes (origin) and a single destination postcode.

The output for a single query is:

  $Time
[1] 5352

$Distance
[1] 34289

$Status
[1] "OK"

I actually have 2.5k postcodes to use but whilst I troubleshoot it I have set the iterations to 10. london1 is a dataframe containing a single column with 2500 postcodes in 2500 rows.

This is my attempt so far;

 results <- for(i in 1:10) {
gmapsdistance::set.api.key("xxxxxx")
gmapsdistance::gmapsdistance(origin = "london1[i]"
                              destination = "WC1E 6BT"
                              mode = "transit"
                              dep_date = "2017-04-18"
                              dep_time = "09:00:00")}

When I run this loop I get

results <- for(i in 1:10) { + gmapsdistance::set.api.key("AIzaSyDFebeOppqSyUGSut_eGs8JcjdsgPBo8zk") + gmapsdistance::gmapsdistance(origin = "london1[i]" + destination = "WC1E 6BT" Error: unexpected symbol in: " gmapsdistance::gmapsdistance(origin = "london1[i]" destination" mode = "transit" dep_date = "2017-04-18" dep_time = "09:00:00")} Error: unexpected ')' in " dep_time = "09:00:00")"

My questions are:

1)How can I fix this?

2) How do I need to format this, so the output is a dataframe or matrix containing the origin postcode and journey time

Thanks

There are a few things going on here:

  • "london[i]" needs to be london[i, 1]
  • you need to separate your arguments with commas ,
  • I get an error when using, eg, "WC1E 6BT" , I found it necessary to replace the space with a dash, like "WC1E-6BT"
  • the loop needs to explicitly assign values to elements of results

So your code would look something like:

library(gmapsdistance)
## some example data
london1 <- data.frame(postCode = c('WC1E-7HJ', 'WC1E-6HX', 'WC1E-7HY'))

## make an empty list to be filled in
results <- vector('list', 3)
for(i in 1:3) {
    set.api.key("xxxxxx")
    ## fill in your results list
    results[[i]] <- gmapsdistance(origin = london1[i, 1],
                                  destination = "WC1E-6BT",
                                  mode = "transit",
                                  dep_date = "2017-04-18",
                                  dep_time = "09:00:00")
}

It turns out you don't need a loop---and probably shouldn't---when using gmapsdistance (see the help doc) and the output from multiple inputs also helps in quickly formatting your output into a data.frame :

set.api.key("xxxxxx")
temp1 <- gmapsdistance(origin = london1[, 1],
              destination = "WC1E-6BT",
              mode = "transit",
              dep_date = "2017-04-18",
              dep_time = "09:00:00", 
              combinations = "all")

The above returns a list of data.frame objects, one each for Time , Distance and Status . You can then easily make those into a data.frame containing everything you might want:

res <- data.frame(origin = london1[, 1],
                  desination = 'WC1E-6BT',
                  do.call(data.frame, lapply(temp1, function(x) x[, 2])))

lapply(temp1, function(x) x[, 2]) extracts the needed column from each data.frame in the list, and do.call puts them back together as columns in a new data.frame object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM