Merging data.frame with SpatialPolygonsDataFrame for apartments classification

I want to merge two objects. The first object (base) is a data.frame with information of apartments in Bogotá, including columns as geographic coordinates. The second object (local) contains spatial polygons with the localities in Bogotá. Locality is the main administrative division of the city.

I want to classify each apartment according its locality.

My data.frame has the following structure:

> str(base)
'data.frame':   37800 obs. of  10 variables:
 $ latitude      : num  4.69 4.64 4.81 4.71 4.6 ...
 $ longitude     : num  -74.2 -74.2 -74 -74.1 -74.1 ...
 $ price         : num  1.88e+09 7.80e+07 1.65e+09 9.50e+08 3.80e+08 ...
 $ area          : num  NA 45 280 193 148 195 NA 167 160 NA ...
 $ bedrooms      : int  NA 3 3 3 4 4 4 3 3 NA ...
 $ bathrooms     : int  NA 1 4 4 4 4 4 3 3 NA ...

On the other side, spatial object local is the following:

> local
class       : SpatialPolygonsDataFrame 
features    : 20 
extent      : -74.44978, -73.98653, 3.73103, 4.836779  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=GRS80 +no_defs 
variables   : 6
names       :       LocNombre,          LocAAdmini,   LocArea, LocCodigo, SHAPE_Leng,   SHAPE_Area 
min values  : ANTONIO NARIÑO, Acuerdo 117 de 2003,   2060243,        01, 0.06715788, 0.0001677672 
max values  :            USME,   Acuerdo 9 de 1986, 780968758,        20, 1.91494942, 0.0635491267 

My idea is to obtain something like this:

  longitude latitude   precio       area     bedrooms badrooms  locality
1 -74.16257 4.689394 1.88e+09         NA           NA    NA     Chapinero
2 -74.19299 4.637710 7.80e+07         45            3     1     Kennedy
3 -74.02988 4.807232 1.65e+09        280            3     4     Puente Aranda
4 -74.08040 4.713976 9.50e+08        193            3     4     Kennedy
5 -74.06911 4.602593 3.80e+08        148            4     4     Suba
6 -74.05801 4.725810 7.50e+08        195            4     4     Chapinero

I will grateful with your help.

1 answer

  • answered 2018-07-11 02:49 Rich Pauloo

    1. Convert your object base into a spatial object.

    library(sp)    
    
    xy <- base[, c(1,2)] # coordinates of your data.frame
    
    base_sp <- SpatialPointsDataFrame(coords = xy, data = base,
                                      proj4string = CRS(proj4string(local)))
    

    2. Use raster::extract()

    library(raster)
    raster::extract(local,     # raster or polygon to extract values from
                    base_sp,   # points to extract values at
                    sp = TRUE) # returns a spatial object