I have a short R script in which I read two sets of data:
a shapefile showing the zip codes of a European country
an Excel showing multiple points coordinates throughout the country
I used the points to define buffers around them. These buffers naturally intersect with the zip codes polygons and one polygon can intersect multiple buffers. Here is the code so far:
F_A <- read.xls("F_A.xlsx", sheet = "FA") F_A$lon <- as.numeric(levels(F_A$lon))[F_A$lon] F_A$lat <- as.numeric(levels(F_A$lat))[F_A$lat] F_A <- na.omit(F_A) coords <- cbind(F_A$lon, F_A$lat) pts <- SpatialPointsDataFrame(coords = coords, data = F_A, proj4string = CRS("+proj=longlat +datum=WGS84")) pts_proj <- spTransform(pts, CRS("+init=epsg:3347")) Buffers <- gBuffer(pts_proj, width = 7000, byid = TRUE) Buffers_xy <- spTransform(Buffers, CRS("+proj=longlat +datum=WGS84")) zips <- readOGR(dsn = "Z", layer = "PLZ") zips.wgs84 <- spTransform(zips, CRS("+proj=longlat +datum=WGS84")) zips.wgs84.df <- fortify(zips.wgs84) Polygons <- readOGR(dsn = "Poly", layer = "POLYGON") Polygons.wgs84 <- spTransform(Polygons, CRS("+proj=longlat +datum=WGS84")) Polygons.wgs84.df <- fortify(Polygons.wgs84)
I would like to add a new attribute to the zip code object counting the number of buffers intersecting with each zip code but fail to find the right command. How would one go about doing this?
To be complete, that is a follow up question on a somewhat similar issue I had already asked about (QGIS creating a heatmap) and managed to solve using QGIS.