With utm_RDS being the spatial lines in question, derived from the following shapefile:
library(rgdal)
library(sp)
RDSYS <- readOGR(dsn = "C:/************")
utm_RDS <- spTransform(RDSYS, CRS("+proj=utm +zone=14 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"))
We can then see
> class(utm_RDS)
[1] "SpatialLinesDataFrame"
attr(,"package")
[1] "sp"
> head(utm_RDS)
An object of class "SpatialLinesDataFrame"
Slot "data":
ID NAME TYPE
0 ****** <NA> <NA>
1 ****** *** ST
2 ****** <NA> <NA>
3 ****** <NA> <NA>
4 ****** <NA> <NA>
Among some omitted columns.
I am now interested in being able to select lines to plot and analyze given their type, but always seem to be met with error. For example,
utm_RDS@lines[utm_RDS$TYPE == "HWY"]
length(utm_RDS@lines[utm_RDS$TYPE == "HWY"])
[1] 46925
Which gives me the number of distinct lines in the shapefile.
But,
plot((utm_RDS@lines[utm_RDS$TYPE == "HWY"])) will not work, returns:
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' is a list, but does not have components 'x' and 'y'
and is of class "list".
Further,
> plot((utm_RDS[utm_RDS$TYPE == "HWY"]))
Error in `[.data.frame`(x@data, i, j, ..., drop = FALSE) :
undefined columns selected
Does anyone have a workaround for this?