wer<-SpatialPointsDataFrame(data.frame(x.subset[,"longitude"],x.subset[,"latitude"]),data=data.frame(x.subset)) n <- length(wer) nearest <- character(n) for (i in seq_along(nearest)) { nearest[i] <- names(polys)[which.min(gDistance(wer[i,], polys, byid=TRUE))] }

This may give you warnings if your code is not projected into a planar coordinate system

http://stackoverflow.com/questions/26308426/how-do-i-find-the-polygon-nearest-to-a-point-in-r

Advertisements