0

I'm trying to subset a Spatial Lines data frame, by a character vector I created.
My data looks like this:

> head(All_travs)
  FEATURE_ID  TRAV_No PROJECT_ID SHAPE_LENG SHAPE_LEN
0       7586 TR_220_N          1 0.17261653         0
1       7593 TR_215_N          1 0.10003741         0
2       7594 TR_210_N          1 0.06658912         0
3       7585 TR_220_S          1 0.12964761         0
4      45038     TR_8          1 0.13393442         0
5      45001    TR_14          1 0.20971781         0

As you can see below, All_travs has 72 features to start with

    > All_travs
class       : SpatialLinesDataFrame 
features    : 72 
extent      : 154037.7, 352737.3, -480490.4, -332383.8  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=22 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
variables   : 5
names       : FEATURE_ID, TRAV_No, PROJECT_ID, SHAPE_LENG, SHAPE_LEN 
min values  :       7585,   TR_10,          1, 0.02586690,         0 
max values  :      58694, TR_95_S,          1, 0.73055204,         0 

The closest answer to this that I found is here Filtering a data frame on a vector But for some reason it doesn't work for me:

When I try to subset the data frame by a vector whose elements match some in column Trav_No:

sel<-c("TR_19", "TR_75_N", "TR_85_N", "TR_95_N")
Trav_sel<-subset(All_travs, All_travs$TRAV_No %in% sel)

It retains 0 features...

> Trav_sel
class       : SpatialLinesDataFrame 
features    : 0 
coord. ref. : +proj=utm +zone=22 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
variables   : 5
names       : FEATURE_ID, TRAV_No, PROJECT_ID, SHAPE_LENG, SHAPE_LEN 
>

... but apparently all the values in Trav_sel$TRAV_No

> All_travs$TRAV_No
[1] TR_220_N TR_210_N TR_95_S  TR_20    TR_125  
69 Levels: TR_10 TR_100_N TR_100_S TR_105_N TR_105_S TR_11 TR_110_N TR_110_S TR_115_N TR_115_S ... TR_95_S
> Trav_sel$TRAV_No
factor(0)
69 Levels: TR_10 TR_100_N TR_100_S TR_105_N TR_105_S TR_11 TR_110_N TR_110_S TR_115_N TR_115_S ... TR_95_S

When I try to match I get all NAs

> match<-match(sel, All_travs$TRAV_No,)
> match
 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

I can't understand this behaviour, or how to properly filter on this vector I have.Any suggestions?

Community
  • 1
  • 1
KasiaM
  • 1
  • 1
  • A SpatialLinesDataFrame is not a base R object. It would be helpful if you would mention the packages that you are using in your question. – lmo Jul 08 '16 at 13:00
  • In your subset: `Trav_sel<-subset(All_travs, All_travs$TRAV_No %in% sel) `: remove the `All_travs$` -> `Trav_sel<-subset(All_travs, TRAV_No %in% sel)`, – bVa Jul 08 '16 at 13:00
  • Thank you bVa, that was exactly it. – KasiaM Jul 09 '16 at 17:49

0 Answers0