sp
provides a shorter form to select features based on spatial intersection, following the OP example:
pts[ply,]
as of:
points(pts[ply,], col = 'red')
Behind the scenes this is short for
pts[!is.na(over(pts, geometry(ply))),]
The thing to note is that there is a geometry
method that drops attributes: over
changes behaviour if itits second argument has attributes or not (this was OP's confusion). This works accross all Spatial* classes in sp
, although some over
methods require rgeos
, see this vignette for more optionsdetails, e.g. the case of multiple matches for overlapping polygons.