0

I have a data frame that looks like this (where I use GP_loc and Hosp_loc as "from" and "to"):

structure(list(Hosp = c("RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
"RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST"), 
    Hosplat = c(52.4532708, 52.4532708, 52.4532708, 52.4532708, 
    52.4532708, 52.4532708, 52.4532708, 52.4532708, 52.4532708, 
    52.4532708, 52.4532708), Hosplon = c(-1.936283476, -1.936283476, 
    -1.936283476, -1.936283476, -1.936283476, -1.936283476, -1.936283476, 
    -1.936283476, -1.936283476, -1.936283476, -1.936283476), 
    x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), GP = c("A81004 - WOODLANDS ROAD SURGERY", 
    "A81017 - WOODBRIDGE PRACTICE", "A81021 - NORMANBY MEDICAL CENTRE", 
    "A81022 - HILLSIDE PRACTICE", "A81025 - THE DOVECOT SURGERY", 
    "A81031 - HAVELOCK GRANGE PRACTICE", "A81035 - NEWLANDS MEDICAL CENTRE", 
    "A81046 - WOODLANDS FAMILY MEDICAL CENTRE", "A81063 - THE HEADLAND MEDICAL CENTRE", 
    "A81064 - THE DISCOVERY PRACTICE", "A81618 - HUNTCLIFF SURGERY"
    ), GPlat = c(54.571614, 54.53808, 54.570665, 54.563124, 54.561689, 
    54.682334, 54.571908, 54.561397, 54.696642, 54.57537, 54.5828
    ), GPlon = c(-1.232274, -1.292145, -1.167559, -0.980768, 
    -1.318938, -1.214149, -1.226236, -1.321186, -1.183105, -1.236504, 
    -0.974691), Hosp_loc = c("52.45327 -1.936283", "52.45327 -1.936283", 
    "52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283", 
    "52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283", 
    "52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283"
    ), GP_loc = c("54.571614 -1.232274", "54.53808 -1.292145", 
    "54.570665 -1.167559", "54.563124 -.980768", "54.561689 -1.318938", 
    "54.682334 -1.214149", "54.571908 -1.226236", "54.561397 -1.321186", 
    "54.696642 -1.183105", "54.57537 -1.236504", "54.5828 -.974691"
    )), .Names = c("Hosp", "Hosplat", "Hosplon", "x", "GP", "GPlat", 
"GPlon", "Hosp_loc", "GP_loc"), class = "data.frame", row.names = c(NA, 
-11L))

and I usually compute the distances like this:

library(ggmap)
results <- mapdist(drivetime$GP_loc, drivetime$Hosp_loc, mode = "driving", override_limit = TRUE) 

In order to avoid Google Maps API limitations of 100 queries/command I was thinking I could create a loop that would say "Compute the distance from GP_loc to Hosp_loc, one at a time and every 2 minutes".

A bit like this:

for (i in 1:10) {
results4 <- mapdist(drivetime$GP_loc[i], drivetime$Hosp_loc[i], mode = "driving", override_limit = TRUE)  
Sys.sleep(10)
}

I am new to R so please be tolerant! Any suggestions? Thanks!!

pdx
  • 303
  • 7
  • 18
  • why not? have you tried? the limit [seems to be](https://developers.google.com/maps/documentation/geocoding/usage-limits) 2500 queries per day though, not 100 every 2 minuts – Vincent Bonhomme Apr 07 '16 at 11:38
  • Hi @VincentBonhomme the limit is also 100 queries at a time. – pdx Apr 07 '16 at 11:45
  • And yes I have tried to run this loom but it says `Error in mapdist(drivetime$GP_loc[i], drivetime$Hosp_loc[i], mode = "driving", : object 'i' not found` – pdx Apr 07 '16 at 11:48
  • 2
    well, the correct syntax should be: `for (i in 1:1903)` see `?Control` – Vincent Bonhomme Apr 07 '16 at 11:51
  • @VincentBonhomme, thanks it worked with: `for (i in 1:10) { results4 <- mapdist(drivetime$GP_loc[i], drivetime$Hosp_loc[i], mode = "driving", override_limit = TRUE) Sys.sleep(10) }` However, the loop works for the first row and that's it, no follow up! Do I need to add something more? – pdx Apr 07 '16 at 12:17
  • perhaps the Sys.sleep(10) is not enough or you may be blocked by the API. Try outside the loop. – Vincent Bonhomme Apr 07 '16 at 12:22
  • I have tried the loop without `Sys.sleep()` and for just 10 queries and I start thinking that the problem is the loop in itself that doesn't work :( – pdx Apr 07 '16 at 12:56
  • the problem is that your example is not [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). so we can't tell if the problem is related to `results`, the `loop`, the `API`. – Vincent Bonhomme Apr 07 '16 at 12:57
  • I have edited the post - hopefully it'll be reproducible? – pdx Apr 07 '16 at 13:13
  • I make it an answer then – Vincent Bonhomme Apr 07 '16 at 14:01

1 Answers1

0

Following the clarifications in the comments, here is a reproducible answer. You can loop and: i) store intermediate results within a list, ii) wait a bit, then rbind the entire list to have a friendly data.frame

library(ggmap)
drivetime <- structure(list(Hosp = c("RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
                        "RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
                        "RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
                        "RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
                        "RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
                        "RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
                        "RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
                        "RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
                        "RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
                        "RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST", 
                        "RRK - UNIVERSITY HOSPITALS BIRMINGHAM NHS FOUNDATION TRUST"), 
               Hosplat = c(52.4532708, 52.4532708, 52.4532708, 52.4532708, 
                           52.4532708, 52.4532708, 52.4532708, 52.4532708, 52.4532708, 
                           52.4532708, 52.4532708), Hosplon = c(-1.936283476, -1.936283476, 
                                                                -1.936283476, -1.936283476, -1.936283476, -1.936283476, -1.936283476, 
                                                                -1.936283476, -1.936283476, -1.936283476, -1.936283476), 
               x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), GP = c("A81004 - WOODLANDS ROAD SURGERY", 
                                                                         "A81017 - WOODBRIDGE PRACTICE", "A81021 - NORMANBY MEDICAL CENTRE", 
                                                                         "A81022 - HILLSIDE PRACTICE", "A81025 - THE DOVECOT SURGERY", 
                                                                         "A81031 - HAVELOCK GRANGE PRACTICE", "A81035 - NEWLANDS MEDICAL CENTRE", 
                                                                         "A81046 - WOODLANDS FAMILY MEDICAL CENTRE", "A81063 - THE HEADLAND MEDICAL CENTRE", 
                                                                         "A81064 - THE DISCOVERY PRACTICE", "A81618 - HUNTCLIFF SURGERY"
               ), GPlat = c(54.571614, 54.53808, 54.570665, 54.563124, 54.561689, 
                            54.682334, 54.571908, 54.561397, 54.696642, 54.57537, 54.5828
               ), GPlon = c(-1.232274, -1.292145, -1.167559, -0.980768, 
                            -1.318938, -1.214149, -1.226236, -1.321186, -1.183105, -1.236504, 
                            -0.974691), Hosp_loc = c("52.45327 -1.936283", "52.45327 -1.936283", 
                                                     "52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283", 
                                                     "52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283", 
                                                     "52.45327 -1.936283", "52.45327 -1.936283", "52.45327 -1.936283"
                            ), GP_loc = c("54.571614 -1.232274", "54.53808 -1.292145", 
                                          "54.570665 -1.167559", "54.563124 -.980768", "54.561689 -1.318938", 
                                          "54.682334 -1.214149", "54.571908 -1.226236", "54.561397 -1.321186", 
                                          "54.696642 -1.183105", "54.57537 -1.236504", "54.5828 -.974691"
                            )), .Names = c("Hosp", "Hosplat", "Hosplon", "x", "GP", "GPlat", 
                                           "GPlon", "Hosp_loc", "GP_loc"), class = "data.frame", row.names = c(NA, 
                                                                                                               -11L))

Here is the loop:

res <- vector("list")
for (i in 1:10) { # could be 1:nrow(drivetime)
  res[[i]] <- mapdist(drivetime$GP_loc[i], drivetime$Hosp_loc[i], mode = "driving", override_limit = TRUE)  
  Sys.sleep(10)
}
res_df <- do.call(rbind, res)

And the result:

> res_df
from                 to      m      km    miles seconds  minutes    hours
1  54.571614 -1.232274 52.45327 -1.936283 286142 286.142 177.8086   11061 184.3500 3.072500
2   54.53808 -1.292145 52.45327 -1.936283 281690 281.690 175.0422   10908 181.8000 3.030000
3  54.570665 -1.167559 52.45327 -1.936283 291233 291.233 180.9722   11244 187.4000 3.123333
4   54.563124 -.980768 52.45327 -1.936283 302302 302.302 187.8505   11748 195.8000 3.263333
5  54.561689 -1.318938 52.45327 -1.936283 287334 287.334 178.5493   11079 184.6500 3.077500
6  54.682334 -1.214149 52.45327 -1.936283 301669 301.669 187.4571   11843 197.3833 3.289722
7  54.571908 -1.226236 52.45327 -1.936283 286680 286.680 178.1430   10956 182.6000 3.043333
8  54.561397 -1.321186 52.45327 -1.936283 287460 287.460 178.6276   11109 185.1500 3.085833
9  54.696642 -1.183105 52.45327 -1.936283 309272 309.272 192.1816   12062 201.0333 3.350556
10  54.57537 -1.236504 52.45327 -1.936283 285644 285.644 177.4992   11001 183.3500 3.055833
Vincent Bonhomme
  • 7,235
  • 2
  • 27
  • 38
  • you are A STAR!!!! Thank you so much for your patience and dedication! Just to make it pedagogical if I had to translate your loop in normal language, what would it be? "For every row i from 1 to 10, assign the `mapdist()` command every 10 seconds to `res[[i]]`"? – pdx Apr 07 '16 at 14:43
  • Sorry to abuse from your time but I'd be grateful if you could explain what `do.call(rbind, vectorname)` means – pdx Apr 07 '16 at 14:45
  • Your "normal language is perfect". Then, `do.call`, applies the function `rbind` on the entire list. So all lines within your list are binded together, rowwise, which result in a `data.frame` – Vincent Bonhomme Apr 07 '16 at 14:47
  • Hi! I left this code to run over night: `res <- vector("list") for (i in 1:nrow(drivetime)) { # could be 1:nrow(drivetime) res[[i]] <- mapdist(drivetime$GP_loc[i], drivetime$Hosp_loc[i], mode = "driving", override_limit = TRUE) Sys.sleep(1) } res_df <- do.call(rbind, res)` ... And at some point it stoped and said `matching was not perfect, returning what was found. Error in `*tmp*`[[c(1, 1)]] : no such index at level 1` – pdx Apr 08 '16 at 08:36
  • 1
  • It stopped at line 893 but now it stops at line 8. Is this correct? `res <- vector("list") for (i in 1:nrow(drivetime)) { # could be 1:nrow(drivetime) cat("Working on ", i ,"\n") res[[i]] <- mapdist(drivetime$GP_loc[i], drivetime$Hosp_loc[i], mode = "driving", override_limit = TRUE) Sys.sleep(1) }` – pdx Apr 08 '16 at 08:49
  • and now it doesn't even run anymore :/ – pdx Apr 08 '16 at 08:59
  • perhaps the API is blocking you now? – Vincent Bonhomme Apr 08 '16 at 11:05
  • It says `6 .fun(piece, ...) 5 (function (i) { piece <- pieces[[i]] if (.inform) { ... 4 loop_apply(n, do.ply) 3 llply(.data = pieces, .fun = .fun, ..., .progress = .progress, .inform = .inform, .parallel = .parallel, .paropts = .paropts) 2 dlply(from_to_df, "from", getdists) 1 mapdist(drivetime$GP_loc[i], drivetime$Hosp_loc[i], mode = "driving", override_limit = TRUE) ` – pdx Apr 08 '16 at 14:13
  • It's becoming hard to help now. Can't reproduce your code. – Vincent Bonhomme Apr 08 '16 at 15:33