The Background
I have an issue for which a number of solution pathways are possible, but I am convinced there is an as-yet-undiscovered elegant solution leveraging purrr.
The Example Code
I have a large data frame as follows, for which I have included an example below:
library(tibble)
library(ggmap)
library(purrr)
library(dplyr)
# Define Example Data
df <- frame_data(
~Street, ~City, ~State, ~Zip, ~lon, ~lat,
"226 W 46th St", "New York", "New York", 10036, -73.9867, 40.75902,
"5th Ave", "New York", "New York", 10022, NA, NA,
"75 Broadway", "New York", "New York", 10006, -74.01205, 40.70814,
"350 5th Ave", "New York", "New York", 10118, -73.98566, 40.74871,
"20 Sagamore Hill Rd", "Oyster Bay", "New York", 11771, NA, NA,
"45 Rockefeller Plaza", "New York", "New York", 10111, -73.97771, 40.75915
)
The Challenge
I would like to geotag all locations for which the lon
and lat
columns are currently NA
. There are many ways I could go about this, one of which is shown below:
# Safe Code is Great Code
safe_geocode <- safely(geocode)
# Identify Data to be Geotagged by Absence of lon and lat
data_to_be_geotagged <- df %>% filter(is.na(lon) | is.na(lat))
# GeoTag Addresses of Missing Data Points
fullAddress <- paste(data_to_be_geotagged$Street,
data_to_be_geotagged$City,
data_to_be_geotagged$State,
data_to_be_geotagged$Zip,
sep = ", ")
fullAddress %>%
map(safe_geocode) %>%
map("result") %>%
plyr::ldply()
The Question
While I can get the above to work, and even wrangle the newly identified lon
and lat
coordinates back into the original data frame, the whole scheme feels dirty. I am convinced there is an elegant way to leverage piping and purrr to go through the data-frame and conditionally geotag the locations based on the absence of lon
and lat
.
I have been down a number of rabbit holes including purrr::pmap
in an attempt to walk through multiple columns in parallel when constructing the full address (As well as rowwise()
and by_row()
). Nevertheless, I fall short in constructing anything that would qualify as an elegant solution.
Any insight provided would be most appreciated.
map_if
within amutate
would be helpful here, but it quickly became clear that I don't quite understand how it works. So far the only thing I've come up with usesunnest
after amap
-within-mutate
but doesn't use purrr at all other than that. – Talithatalk