I am kinda new to R and I am stuck on this problem for a while.
The problem is as following:
I have 3 files. 1. Properties_file --> This file contains point data of several properties 2. Bus_Station_Points --> This file contains point data of bus stations 3. b --> a buffer of 402 meters around each station. What I want to do is, I want to know for each Properties_file point whether it is located within one (or more) of the buffer zones and if so, which ones these are. This should be displayed in Properties_file$over.
I have already come quite far in running this with the code below. However, somehow I cannot unlist the output in a proper way, and therefore I cannot save the output as a csv or xlxs file.
I know that I somehow should change the empty values in the list to 'NA' or '0'. But somehow the 'normal' function below does not have any effect.
Properties_file$over[!sapply(!Properties_file$over, length)] <- NA
And if I run an unlist on the data, all the empty rows and columns that display more than 1 ID number disappear.
It is really important to me that the output data keeps related to the points in Properties_file so that I know exactly which points are located in which polygon/buffer.
This is the code that I have been using:
b <- raster::buffer(Bus_Stations_Point , 402 , dissolve=FALSE)
polys <- b@polygons[[1]]@Polygons
pl <- vector("list", length(polys))
for (i in 1:length(polys)) { pl[i] <- Polygons(list(polys[[i]]), i) }
b.spolys <- SpatialPolygons(pl)
row.ids=sapply(slot(b.spolys, "polygons"), function(i) slot(i, "ID"))
b <- SpatialPolygonsDataFrame(b.spolys, data.frame(FID=as.numeric(row.ids)) )
raster::crs(b) <- raster::crs(Properties_file)
Properties_file$over <- sp::over(Properties_file , b , returnList = TRUE,)
This is an example of the output after I have run the over function.
These are the 2 functions that I have run afterwards but don't give the desired result.
Properties_file$over[!sapply(Properties_file$over, length)] <- NA
Properties_file$over <- matrix(unlist(Properties_file$over), nrow =
length(Properties_file$over), ncol = max(vapply(Properties_file$over , length
, 0)), byrow = TRUE)
I hope you guys can help me out.
Thanks already.
Regards,
Ruben