当列包含因子时,使用OR子集数据框 [英] Subset a data frame using OR when the column contains a factor
本文介绍了当列包含因子时,使用OR子集数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在R中制作一个数据帧的子集,该子集基于一列因子中的一个或另一个值,但看来我不能将|
与因子值一起使用.
I would like to make a subset of a data frame in R that is based on one OR another value in a column of factors but it seems I cannot use |
with factor values.
示例:
# fake data
x <- sample(1:100, 9)
nm <- c("a", "a", "a", "b", "b", "b", "c", "c", "c")
fake <- cbind(as.data.frame(nm), as.data.frame(x))
# subset fake to only rows with name equal to a or b
fake.trunk <- fake[fake$nm == "a" | "b", ]
产生错误:
Error in fake$nm == "a" | "b" :
operations are possible only for numeric, logical or complex types
我该怎么做?
很明显,我的实际数据帧在factor列中有3个以上的值,因此仅使用!= "c"
无效.
Obviously my actual data frame has more than 3 values in the factor column so just using != "c"
won't work.
推荐答案
您需要fake.trunk <- fake[fake$nm == "a" | fake$nm == "b", ]
.一种更简洁的写法(尤其是具有两个以上的条件)是:
You need fake.trunk <- fake[fake$nm == "a" | fake$nm == "b", ]
. A more concise way of writing that (especially with more than two conditions) is:
fake[ fake$nm %in% c("a","b"), ]
这篇关于当列包含因子时,使用OR子集数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文