I have a dataset of Longitude and Latitude values and I want to check whether they are in the Greater Toronto Area. Alternative, labeling them with the Closest Census Metrop
Here is a working example of how to do this with the rgdal package. There are plenty of other ways to do this. I provided a link to where you can get a Toronto shape file if you do not already have one. If you have any questions please let me know.
library(rgdal)
myTestDF <- data.frame(MyDate = c("A","Toronto","C"),
latitude = c(74.3224,43.686094, 88.9237),
longitude = c(66.2222, -79.401350, -49.0074))
setwd("C:/WhereShapeFilesAre")
#Download shape file from open data site and unzip contents into a folder this example uses the wgs84 format.
#http://www1.toronto.ca/wps/portal/contentonly?vgnextoid=c1a6e72ced779310VgnVCM1000003dd60f89RCRD&vgnextchannel=75d6e03bb8d1e310VgnVCM10000071d60f89RCRD
TorontoShape<- readOGR(".", "citygcs_regional_mun_wgs84")
myTestPoints <- myTestDF
coordinates(myTestPoints) <- ~ longitude + latitude
proj4string(myTestPoints) <- proj4string(TorontoShape)
cbind(myTestDF, over(myTestPoints, TorontoShape))