Merging SpatialPointsDataFrame to SpatialPolygonsDataFrame in R
I am trying to perform a spatial join between SpatialPointsDataFrame and SpatialPolygonsDataFrame.
I have two sets of data named California_shp and Houses. Their information is as follow:
dput(head(California_shp))
structure(list(NAME = 1:6), row.names = c(NA, 6L), class = "data.frame")
class(California_shp)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
dput(head(Houses))
structure(list(houseValue = c(452600L, 358500L, 352100L, 341300L,
342200L, 269700L), income = c(8.3252, 8.3014, 7.2574, 5.6431,
3.8462, 4.0368), houseAge = c(41L, 21L, 52L, 52L, 52L, 52L),
rooms = c(880L, 7099L, 1467L, 1274L, 1627L, 919L), bedrooms = c(129L,
1106L, 190L, 235L, 280L, 213L), population = c(322L, 2401L,
496L, 558L, 565L, 413L), households = c(126L, 1138L, 177L,
219L, 259L, 193L), latitude = c(37.88, 37.86, 37.85, 37.85,
37.85, 37.85), longitude = c(-122.23, -122.22, -122.24, -122.25,
-122.25, -122.25)), row.names = c(NA, 6L), class = "data.frame")
class(Houses)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"
How by using the point-in-polygon operation and the mean function, I can merge the Houses object to California_shp?
I have written and runed the following codes in R:
library(spatialEco)
library(sp)
head(California_shp@data)
NAME
1 Alameda
2 Alpine
3 Amador
4 Butte
5 Calaveras
6 Colusa
head(Houses@data)
houseValue income houseAge rooms bedrooms population households latitude longitude
1 452600 8.3252 41 880 129 322 126 37.88 -122.23
2 358500 8.3014 21 7099 1106 2401 1138 37.86 -122.22
3 352100 7.2574 52 1467 190 496 177 37.85 -122.24
4 341300 5.6431 52 1274 235 558 219 37.85 -122.25
5 342200 3.8462 52 1627 280 565 259 37.85 -122.25
6 269700 4.0368 52 919 213 413 193 37.85 -122.25
pts.poly