1

I am having trouble understanding the outputs when using this google_distance function. When using mydist() in ggmap I would get the number of miles, minutes, hours that it would take to get to point A to point B.

Now my output looks like this when I use google_distance. Can anyone help explain what each of the numbers is referring to?

$rows
                                                            elements
1 791 km, 790588, 7 hours 28 mins, 26859, 7 hours 35 mins, 27286, OK

My code is as follows:

results <- google_distance(origins = list(c(26.19660, -98.23591)),
                          destinations = list(c(31.62327, -94.64276)), 
                          mode = "driving", key = key, simplify = TRUE)
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
HHK
  • 11
  • 2
  • A code sample or link to the R notebook would be helpful. I looked for the docs and had trouble finding the answer, so there's a real question here. For the question of WHY the docs didn't have the answer, that I can't answer. I'm hoping this gets answered with a proper link to the docs, as that would be helpful. – Henry Crutcher Feb 13 '17 at 16:40
  • Thank you! I added my code to my question. – HHK Feb 13 '17 at 16:47

1 Answers1

4

What you're seeing is the standard JSON response, but simplified into a data.frame (as per the simplify = TRUE argument)

If you look one level deeper at your response, you'll get the description of those valeus

results$rows$elements
# [[1]]
#   distance.text distance.value   duration.text duration.value duration_in_traffic.text duration_in_traffic.value
# 1        791 km         790588 7 hours 28 mins          26859          7 hours 28 mins                     26906

where

  • distance.value is in metres
  • duration.value is in seconds

Similarly, looking at the structure of the result object, you'll see all the JSON elements

str(results)
# List of 4
# $ destination_addresses: chr "805 E College St, Nacogdoches, TX, USA"
# $ origin_addresses     : chr "1400-1498 W Houston Ave, McAllen, TX 78501, USA"
# $ rows                 :'data.frame': 1 obs. of  1 variable:
#   ..$ elements:List of 1
# .. ..$ :'data.frame': 1 obs. of  4 variables:
#   .. .. ..$ distance           :'data.frame': 1 obs. of  2 variables:
#   .. .. .. ..$ text : chr "791 km"
# .. .. .. ..$ value: int 790588
# .. .. ..$ duration           :'data.frame':   1 obs. of  2 variables:
#   .. .. .. ..$ text : chr "7 hours 28 mins"
# .. .. .. ..$ value: int 26859
# .. .. ..$ duration_in_traffic:'data.frame':   1 obs. of  2 variables:
#   .. .. .. ..$ text : chr "7 hours 28 mins"
# .. .. .. ..$ value: int 26906
# .. .. ..$ status             : chr "OK"
# $ status               : chr "OK"

Further Reference:

Google Developers Guide: Distance Matrix

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • what happens when I pass vector of values in origin as well as destination? What I have is `result <- google_distance(tmp[, c('origin_lat', 'origin_lon')], tmp[, c('destination_lat', 'destination_lon')], key = api_key)` I see that `result$rows$elements` returns a list of length equal to number of rows in `tmp` and in each list there is a dataframe which again has same number of rows as `tmp`. What I was looking for is distance between `tmp[1, c('origin_lat', 'origin_lon')]` and `tmp[1, c('destination_lat', 'destination_lon')]` and same for row 2 and so on. Should I use a different function? – Ronak Shah Apr 06 '21 at 04:40
  • The Distance Matrix (as returned from `google_distance()`) returns the distance between all combinations of origins & destinations. If you want a 'paired' response you'll have to write a loop (and probably use the `google_directions()` to get more details) – SymbolixAU Apr 06 '21 at 05:53
  • Something like [this answer](https://stackoverflow.com/a/41070879/5977215) (there are others floating around too) – SymbolixAU Apr 06 '21 at 05:55
  • But, if you only want the distance and don't care about directions or times or anything, then I recommend `geodist::geodist_vec(..., paired = TRUE)` – SymbolixAU Apr 06 '21 at 05:56