1

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?

1 Answer 1

2

Spatial*DataFrame objects inherit methods from dataframes, so your last attempt was almost correct; it's just missing a coma to indicate that you are filtering rows. It should be:

plot(utm_RDS[utm_RDS$TYPE == "HWY",])
1
  • That works, thank you! I assume this is because R expects 2D, planar coordinates. Commented Aug 25, 2017 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.