2

I have the dataframe below

> head(cities)
  location_id longitude latitude
2       1  -79.3942  43.6724
3       2 -123.1070  49.2540
4       3  -71.0589  42.3601
5       4  -84.3880  33.7490
6       5  -97.7431  30.2672
7       6  -76.6122  39.2904

I downloaded the census division boundary shapefile from statcan which I read using

shapefile <- readOGR(".", "gcd_000a11a_e")

http://www.statcan.gc.ca/access_acces/alternative_alternatif.action?l=eng&dispext=zip&teng=gcd_000a11a_e.zip&k=%20%20%20%2014372&loc=http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/gcd_000a11a_e.zip

I'm trying to find the census division each pair of coordinates fall into.

> summary(shapefile)
Object of class SpatialPolygonsDataFrame
Coordinates:
         min       max
x -141.01807 -52.58230
y   41.67695  89.99943
Is projected: FALSE 
proj4string :
[+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0]
Data attributes:
    CDUID              CDNAME             CDTYPE             PRUID              PRNAME         
 Length:293         Length:293         Length:293         Length:293         Length:293        
 Class :character   Class :character   Class :character   Class :character   Class :character  
 Mode  :character   Mode  :character   Mode  :character   Mode  :character   Mode  :character  
> 
1

I think I've managed to get it to work using

# -
coordinates(cities) <- ~longitude+latitude
proj4string(cities) <- proj4string(shapefile)


dataset <- over(cities, shapefile) %>%
  cbind(cities.original)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.