How to use purrr for extracting elements from a list?

Viewed 3200

I am trying to use purrr to access the level formatted_address from this list:

tt <- lapply(c('Austin, Texas', 'Houston, Texas'),  
function(x) ggmap::geocode(x, output='all', nameType = 'long', sensor = F))

The final output should be a vector of the elements from $ formatted_address : chr "Austin, TX, USA", namely c('Austin, TX, USA', 'Houston, TX, USA')

To start off, I was trying to grab the record for the first of the two elements of the list using standard indexing, and then converting it to purrr following this tutorial.

For instance, I thought

> tt[[1]]$results[[1]]$formatted_address
[1] "Austin, TX, USA"

Would be equivalent to the code below, but it isn't:

> map(tt[[1]]$results[[1]], 'formatted_address')
Error in map.poly(database, regions, exact, xlim, ylim, boundary, interior,  : 
  no recognized region names

Why aren't these two lines of code equivalent? And how could I use map or map_chr to create a vector of records from formatted_address?

1 Answers
Related