I'm attempting to discern whether a given lat/long coordinate falls within a particular US state. I have used the maps
package to obtain lat/long coordinates for state borders. I suspect there is a way to ask whether the coordinate falls within the range of lat and long values encompassed by each state, but I have not figured out an elegant solution.
Basically, this is a way to identify whether a point falls within a region without having to know the region's name (since the maps
package is pretty idiosyncratic in terms of naming regions, e.g., USSR), but still having the ability to extract the region name at a later point (for shading, etc.).
#Example coordinates
points=data.frame(lat=42,long=-100)
#Load US map
library(maps)
states=map_data("state")
#Summarize value of lat and long values for each region
library(plyr)
ddply(states,c("region"),function(x) return(c( range(x$long), range(x$lat) )) )
In the end, I want the data.frame
states
to have a final column giving true/false
as to whether the points occur in that region. So, in the above example, all entries where region=="nebraska"
would have true
.