I've imported a shapefile into R, and joined it to a table. My shapefile contains all the census ids, while my table only contains selected census ids. I'm now trying delete all the rows didn't get a match.

This is what my dataset looks like (I'm trying to remove all the rows with NA, so the last two would need to be removed):

1 Answer
1

The informative part of the error is that the data you operating on is an S4 class object and as such contains slots. This means that you need to operate on the appropriate slot "@data" containing your dataframe.

If you want to delete "all" rows with NA values you can just use na.omit on the dataframe slot. This does propgate through the sp object and removes associated points/polygons in the other slots.

shape@data <- na.omit(shape@data)

If you want to remove rows with NA's in a specific column you can use:

shape@data <- shape[shape@data$col != NA ,]

I should also point out that you cannot use the merge function to join to an sp object. The merge function resorts the data during the operation which breaks the internal relationship in the sp object. This is something that is, unfortunately, not widely advertised. To merge a dataframe to the @data slot of an sp object you can use match in this way.

Where; shape is your shape file, IDS is the identifier you want to merge on and OtherData is the dataframe that you want to combine with shape. Note that IDS can be different names in the two datasets but need to actually be the same values (not fuzzy).

For some reason I cannot comment on @Kelly question so I am editing my original answer. Check what version of R and sp are you running? You can run SessionInfo() to find out. The behavior of removing associated objects in the other data slots when manipulating the @data object has only been available in the last couple sp versions. If not running a current version try updating the package with "Update packages" under the packages menu. If running >=Windows Vista be sure to run as administrator. Also look at your before and after object dimensions i.e., dim(shape), which represents the number of rows/cols. The number of rows corresponds with the number of feature objects. You can gut check the results by checking to see if the number of rows in the spatial object match the number of rows in the @data slot i.e., dim(shape); dim(shape@data)

Thanks for your help! I redid the spatial join because I did use a 'merge' instead of 'match'. I've removed all the NA rows, but the shape are still present in the shapefile when I plot it. Any thoughts on why this is happening?
–
KellyMar 7 '13 at 15:53

An amendment to this answer is necessary at sp 1.0-15. An sp specific version of the merge function is now called, when passed an sp class object, that performs correctly given that you perform a one to one match to keep the row dimensions consistent with the associated slots.
–
Jeffrey EvansSep 17 '14 at 0:34