I want to remove the rows that have certain values from the spatial data.frame
since the standard R grep()
seems not propagate through all slots of an sp
class object.
bd@data[- grep("xcluded", bd@data$Notes),]
It gives me an error:
Error: trying to get slot "data" from an object (class "data.frame") that is not an S4 object
I read that sp.na.omit
can remove NA
but don't know how to remove the rows have a certain value(eg. remove rows that have "exclude" in bd@data$Notes). Any suggestions?
Sorry I'm not able to create a shapefile example through R, but I'd try to provide more information of my sp dataframe:
> class(bd)
1"SpatialLinesDataFrame"
attr(,"package")
1 "sp"
class : SpatialLinesDataFrame
features : 8855
extent : 172.6811, 174.5966, -36.36374, -34.42634 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
variables : 134
names : OBJECTID_1, Name_in_us, Unique_ID, RAMM_Road_, iSequence, SH, OBJECTID, ROADID, RoadLength, RoadNameAn, Displaceme, road_id, AvgWidth, pave, CJNEX_urba, ...
min values : 1, 014-0000, 1188496, 0, 0, No, 0, 0, 0, 012-0132 (805), 0-1008m, 0, 0, Concrete, Rural, ...
max values : 5299, ZEALANDIA ST, 2044000001, 2065, 480, Yes, 999, 1683, 45826.7818765, ZIDICH ROAD, 9925-9966m, 3520, 18.2, Unsealed, Urban, ...
Polylines inside the df(bd@lines)
$lines[[1000]]
An object of class "Lines"
Slot "Lines":
[1]
An object of class "Line"
Slot "coords":
[,1] [,2]
[1,] 174.3629 -35.77290
[2,] 174.3627 -35.77281
[3,] 174.3624 -35.77276
bd@data[1000,]
OBJECTID_1 Name_in_us Unique_ID RAMM_Road_ iSequence SH OBJECTID ROADID RoadLength RoadNameAn Displaceme road_id AvgWidth ...
1000 1000 DOMAIN RD 1.16e+08 116 2 No 89 116 94.75686 DOMAIN RD (116) 0-95m ...
The solution:
1. as answer/comment below
2. use bd[- grep("xcluded", bd$Notes),]
instead