简体   繁体   中英

R Subset by Factor level

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?

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

应该管用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM