Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R convert zipcode or lat/long to county

I have a list of locations that contains a city, state, zip, latitude and longitude for each location.

I separately have a list of economic indicators at the county level. I've played with the zipcode package, the ggmap package, and several other free geocoding websites including the US Gazeteer files, but can't seem to find a way to match the two pieces.

Are there currently any packages or other sources that do this?

like image 726
screechOwl Avatar asked Nov 09 '12 21:11

screechOwl


People also ask

How do I use zipcode in R?

If you are not familiar with R, the first line is loading the 'zipcode' package into the current R session. The second line is using 'data' function to extract the 'zipcode' data from the package as a data frame called 'zipcode'. The last line is calling the data frame to return the data.

How do I convert a zip code to latitude and longitude in Excel?

To get the latitude of the address in cell B2, use the formula = GetLatitude(B2) To get the longitude of the address in cell B2, use the formula = GetLongitude(B2) To get both the latitude and longitude of the address in cell B2, use the formula = GetCoordinates(B2)


1 Answers

I ended up using the suggestion from JoshO'Brien mentioned above and found here.

I took his code and changed state to county as shown here:

library(sp) library(maps) library(maptools)  # The single argument to this function, pointsDF, is a data.frame in which: #   - column 1 contains the longitude in degrees (negative in the US) #   - column 2 contains the latitude in degrees  latlong2county <- function(pointsDF) {     # Prepare SpatialPolygons object with one SpatialPolygon     # per county     counties <- map('county', fill=TRUE, col="transparent", plot=FALSE)     IDs <- sapply(strsplit(counties$names, ":"), function(x) x[1])     counties_sp <- map2SpatialPolygons(counties, IDs=IDs,                      proj4string=CRS("+proj=longlat +datum=WGS84"))      # Convert pointsDF to a SpatialPoints object      pointsSP <- SpatialPoints(pointsDF,                      proj4string=CRS("+proj=longlat +datum=WGS84"))      # Use 'over' to get _indices_ of the Polygons object containing each point      indices <- over(pointsSP, counties_sp)      # Return the county names of the Polygons object containing each point     countyNames <- sapply(counties_sp@polygons, function(x) x@ID)     countyNames[indices] }  # Test the function using points in Wisconsin and Oregon. testPoints <- data.frame(x = c(-90, -120), y = c(44, 44))  latlong2county(testPoints) [1] "wisconsin,juneau" "oregon,crook" # IT WORKS 
like image 200
screechOwl Avatar answered Oct 23 '22 21:10

screechOwl