2

I was working with the googleway package and I had a bunch of addresses that I needed to parse out the various components of the addresses that were in a nested list of lists. Loops (not encouraged) and apply functions both seemed confusing and I was not sure if there was a tidy solution. I found the map function (specifically the pluck function that it calls on lists on the backend) could accomplish my goal so I will share my solution.

Problem: I need to pull out certain information about the White House such as

  • Latitude
  • Longitude

You need to set up your Google Cloud API Key with googleway::set_key(API_KEY), but this is just an example of a nested list that I hope someone working with this package will see.

# Address for the White House and the Lincoln Memorial

address_vec <- c(
  "1600 Pennsylvania Ave NW, Washington, DC 20006",
  "2 Lincoln Memorial Cir NW, Washington, DC 20002"
)

address_vec <- pmap(list(address_vec), googleway::google_geocode) 

outputs

[[1]]
[[1]]$results
                                                                                                                                                                                                                                                                                                                             address_components
1 1600, Pennsylvania Avenue Northwest, Northwest Washington, Washington, District of Columbia, United States, 20500, 1600, Pennsylvania Avenue NW, Northwest Washington, Washington, DC, US, 20500, street_number, route, neighborhood, political, locality, political, administrative_area_level_1, political, country, political, postal_code
                                       formatted_address geometry.bounds.northeast.lat
1 1600 Pennsylvania Avenue NW, Washington, DC 20500, USA                       38.8979
  geometry.bounds.northeast.lng geometry.bounds.southwest.lat geometry.bounds.southwest.lng geometry.location.lat
1                     -77.03551                      38.89731                     -77.03796              38.89766
  geometry.location.lng geometry.location_type geometry.viewport.northeast.lat geometry.viewport.northeast.lng
1             -77.03657                ROOFTOP                        38.89895                       -77.03539
  geometry.viewport.southwest.lat geometry.viewport.southwest.lng                    place_id
1                        38.89626                       -77.03808 ChIJGVtI4by3t4kRr51d_Qm_x58
                                      types
1 establishment, point_of_interest, premise

[[1]]$status
[1] "OK"


[[2]]
[[2]]$results
                                                                                                                                                                                                                                                                                                                            address_components
1 2, Lincoln Memorial Circle Northwest, Southwest Washington, Washington, District of Columbia, United States, 20037, 2, Lincoln Memorial Cir NW, Southwest Washington, Washington, DC, US, 20037, street_number, route, neighborhood, political, locality, political, administrative_area_level_1, political, country, political, postal_code
                                     formatted_address geometry.location.lat geometry.location.lng
1 2 Lincoln Memorial Cir NW, Washington, DC 20037, USA              38.88927             -77.05018
  geometry.location_type geometry.viewport.northeast.lat geometry.viewport.northeast.lng
1                ROOFTOP                        38.89062                       -77.04883
  geometry.viewport.southwest.lat geometry.viewport.southwest.lng                    place_id
1                        38.88792                       -77.05152 ChIJgRuEham3t4kRFju4R6De__g
      plus_code.compound_code plus_code.global_code          types
1 VWQX+PW Washington, DC, USA           87C4VWQX+PW street_address

[[2]]$status
[1] "OK"

2 Answers2

1

Here's some code that I got from the Googleway Vignette:

df <- google_geocode(address = "Flinders Street Station",
                     key = key,
                     simplify = TRUE)


geocode_coordinates(df)
#         lat      lng
# 1 -37.81827 144.9671

It looks like what you need to do is:

df <- google_geocode("1600 Pennsylvania Ave")
geocode_coordinates(df)
Big D
  • 15
  • 5
  • Thank you. I set this up to answer my own question about accessing certain elements of lists in general because I hadn't seen this example posted anywhere on here. I guess this part does answer my question about how to access this information if I had just one address. Thank you – Adrian Fletcher Jan 08 '21 at 23:09
1

The solution I came up with is a custom function that can access any section of the list:

geocode_accessor <- function(df, accessor, ...) {
  unlist(map(df, list(accessor, ...)))
}

This has three important parts to understand:

  1. The map function is calling the pluck function for us (it replaces the use of [[ ). You can read more about what is happening here, but just know this lets us access things by name
  2. The "..." in the function's definition as well as in the list allows us to access multiple levels. Again, the use of list() to access further levels in a list is explained in the pluck documentation
  3. The use of unlist converts the list to a vector (what I want in my instance)

Putting this all together, we can get the latitude of the White House & Lincoln Memorial:

geocode_accessor(address_vec, "results", "geometry", "location", "lat")

[1] 38.89766 38.88927