简体   繁体   English

R按因子水平细分

[英]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. 因子级别的数量始终为4,但是这些级别的名称可能会根据数据集而变化。 1 1个

Specifically, the name of the SET1$Bearing factors may change. 具体来说,SET1 $ Bearing因子的名称可能会更改。 For examples "001°" may be "002°" in the next data set. 例如,在下一个数据集中“ 001°”可以是“ 002°”。 Presently I use this code... 目前,我使用此代码...

SET1Bearing1<-SET1[SET1$Bearing=="001°",] 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],]

应该管用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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