0

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

  • Hard to tell bc I don't know what your original data looks like (check this out https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Why don't you try `unnest` from the `tidyr` package? – Ben G Jul 25 '18 at 12:20
  • Thank you for your reply. I have tried to unnest but it gives this error: no applicable method for 'unnest_' applied to an object of class "character" So apparently my column is not a list but a "character". But my original data file is S4. Not sure how to fix it now. – Ruben van Beek Jul 27 '18 at 10:16
  • I think I can help you, but I need to see what your data looks like. May just need to convert the column into a list with either `list` or `as.list` and then using `unnest` after that. – Ben G Jul 27 '18 at 12:58
  • Thank you. I have saved the data as.list and then I was able to convert the empty values into "NA" with df$over == "list(FID = numeric(0))"] <- "NA" Then I used the concat.split function out of the splitstackshape package to divide the output into multiple columns. – Ruben van Beek Jul 31 '18 at 14:43

1 Answers1

0

For those who are wondering. This is how I was able to solve it eventually:

as.list(Properties_file)
Properties_file$over <- sp::over(propp , Airports_Buffer , returnList = TRUE)
Properties_file$over[Properties_file$over == "list(FID = numeric(0))"] <- "NA"
Properties_file$over <- splitstackshape::concat.split(Properties_file , "over" , sep = "," , structure = "compact")

Thanks everybody for your suggestions!