0

I am attempting to formulate code that will subset by factor level not factor name so minimal editing is required to run the code with other data sets. The number of factor levels is always 4, yet the name of those levels may change depending on the data set.1

Specifically, the name of the SET1$Bearing factors may change. For examples "001°" may be "002°" in the next data set. Presently I use this code...

SET1Bearing1<-SET1[SET1$Bearing=="001°",]

Yet this only subsets by the name of the factor. Is there a way to subset by factor level regardless of the factor name?

Malcolm Wells
  • 19
  • 2
  • 8
  • 1
    `levels(SET1$Bearing)` should return the unique values. Then `levels(SET1$Bearing)[1]` would reutrn the first, `levels(SET1$Bearing)[2]` the second, etc. It would probably even be better to to use `split()` to create all the subsets at once. When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 12 '18 at 20:30
  • Hey MrFlick thank you for the tip. Next time I will be sure to include a reproducible example with my desired output. – Malcolm Wells Jun 13 '18 at 12:45

1 Answers1

0
SET1Bearing1<-SET1[SET1$Bearing == levels(SET1$Bering)[4],]

Should work

Eric Yang
  • 2,678
  • 1
  • 12
  • 18