This question already has an answer here:

I have used the over function to retrieve the polygons of a map that contain points. I'am wondering, for my own learning purposes, if there is a way to use the over function to retrieve the polygons that don't have points in them. Is this possible?

Here is some code to return polygons of a map WITH points in them:

coordinates(data) <- ~ long_xcord + lat_ycord

# Set the projection of the SpatialPointsDataFrame using the projection of the shapefile.
proj4string(data) <- proj4string(shapeData)

p <- over(data, shapeData, returnList = TRUE)

p <- do.call(rbind.data.frame, p)

can the above be modified to return the polygons that DO NOT have points in them?

marked as duplicate by Jeffrey Evans, whyzar, Michael Stimson, Fezter Apr 12 '17 at 6:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You can use the results of your over query to select from the original polygon layer, and its easier if you just set returnList to False to get a dataframe e.g.

p <- over(points, polys)
polys_notint <- polys[!(polys$id_field %in% unlist(p$id_field)), ]

Not the answer you're looking for? Browse other questions tagged or ask your own question.