0
votes

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"

bd attributes

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

1
HI Can you provide a minimal example. stackoverflow.com/help/minimal-reproducible-exampleSubhasish1315
Sorry, I'm not able to create a shapefile in r.. like polylines with some attributes. I tried to provide more information of my sp df if this helps... otherwise, I think I need to upload part of my datapolaris

1 Answers

0
votes

I don't believe at this point that bd is a spatial object any more, or this:

bd@data[- grep("xcluded", bd@data$Notes),]

Is not the line producing this error:

Error: trying to get slot "data" from an object (class "data.frame") that is not an S4 object

The error says it is trying to get the data slot from a data frame. Its nothing todo with grep or subsetting.

I can replicate that error message:

> d = data.frame(x=1:10)
> d@data
Error: trying to get slot "data" from an object (class "data.frame") that is not an S4 object 
> 

but I can't replicate that with a spatial sp-class object:

> Sldf
class       : SpatialLinesDataFrame 
features    : 2 
extent      : 1, 3.05, 1, 3.05  (xmin, xmax, ymin, ymax)
crs         : NA 
variables   : 1
names       :  Z 
min values  : AA 
max values  : AB 
> Sldf@data
   Z
1 AA
2 AB
> Sldf@data[-grep("B",Sldf@data$Z),,drop=FALSE]
   Z
1 AA