rshapefilecentroidrgeo-shapefile

Receiving Error Message when using command over()


I have two shape files, shp1 and shp2. After calculating the centroid of the former, I'd like to see where the centroid lies in the latter.

My setup does:

shp1@data$centroid <- gCentroid(shp1, byid = TRUE)
foo <- over(shp1$centroid, shp2)

Upon executing the second line, I receive the following error message:

Error in xj[i] : NAs not permitted in row index

Any idea what I should be looking for to avoid this error? (note: grep("NA", row.names(shp1)) returns integer(0), also for the other shape file.)


Solution

  • Just figured out why this error occurs:

    In addition to shp1@data$centroid <- gCentroid(shp1, byid = TRUE), I also did shp2@data$centroid <- gCentroid(shp2, byid = TRUE) before calling foo <- over(shp1$centroid, shp2), which apparently caused the error.