I have a data set that contained lat/long information for different point locations, and I would like to know which city and state are associated with each point.
Following this example I used the revgeocode
function from ggmap
to obtain a street address for each location, producing the data frame below:
df <- structure(list(PointID = c(1787L, 2805L, 3025L, 3027L, 3028L,
3029L, 3030L, 3031L, 3033L), Latitude = c(38.36648102, 36.19548585,
43.419774, 43.437222, 43.454722, 43.452643, 43.411949, 43.255479,
43.261464), Longitude = c(-76.4802046, -94.21554661, -87.960399,
-88.018333, -87.974722, -87.978542, -87.94149, -87.986433, -87.968612
), Address = structure(c(2L, 8L, 5L, 3L, 9L, 7L, 4L, 1L, 6L), .Label = c("13004 N Thomas Dr, Mequon, WI 53097, USA",
"2160 Turner Rd, Lusby, MD 20657, USA", "2805 County Rd Y, Saukville, WI 53080, USA",
"3701-3739 County Hwy W, Saukville, WI 53080, USA", "3907 Echo Ln, Saukville, WI 53080, USA",
"4823 W Bonniwell Rd, Mequon, WI 53097, USA", "5100-5260 County Rd I, Saukville, WI 53080, USA",
"7948 W Gibbs Rd, Springdale, AR 72762, USA", "River Park Rd, Saukville, WI 53080, USA"
), class = "factor")), row.names = c(NA, -9L), class = "data.frame", .Names = c("PointID",
"Latitude", "Longitude", "Address"))
I would like to use R to extract the city/state information from the full street address, and create two columns to store this information ("City" and "State).
I'm assuming the stringr
package is the way to go, but I'm not sure how to go about using it. The example above used the following code to extract the zip code (named "result" in that example). Their data set:
# ID Longitude Latitude result
# 1 311175 41.29844 -72.92918 16 Church Street South, New Haven, CT 06519, USA
# 2 292058 41.93694 -87.66984 1632 West Nelson Street, Chicago, IL 60657, USA
# 3 12979 37.58096 -77.47144 2077-2199 Seddon Way, Richmond, VA 23230, USA
And code to extract the zipcode:
library(stringr)
data$zipcode <- substr(str_extract(data$result," [0-9]{5}, .+"),2,6)
data[,-4]
Is it possible to easily modify the above code to get the city and state data?