从data.frame中排除行,具体取决于data.frame

时间:2017-07-06 06:14:51

标签: r dataframe

我有以下模拟data.frame:

(请注意我已经重写了大部分问题,反映了akrun对我最初问题的回答)

set.seed(22)
df <- data.frame(f1 = rep("a", 20), f2 = factor(sample(c("yes", "no", "maybe", "maybenot"), 20, replace = T)), f3 = factor(sample(c("yes", "no"), 20, replace = T)), f4 = factor(sample(c("yes", "no"), 20, replace = T)))

   f1       f2  f3  f4
1   a    maybe yes yes
2   a       no yes yes
3   a      yes  no  no
4   a    maybe yes  no
5   a    maybe  no yes
6   a maybenot  no yes
...

我想在yes中排除显示df$f2的所有行,并在no中显示df$f3,或df$f4。如果我手动将值转换为0和1(yes中除df$f2之外的所有内容都为0),我可以按照akrun的建议使用rowSums。我目前的解决方案是引入一个名为df$exclude的虚拟列,如下所示,然后subset上的df$exclude

df$exclude <- "no"
df[df$f2 != "yes" | df$f3 == "no" | df$f4 == "no",]$exclude <- "yes"    
df <- subset(df, exclude == "no")

这可以更简洁地完成,例如没有事先转换列f2,f3和f3,或者使用lapply(以某种方式与subset结合,可能还有匿名函数)?

提前感谢您的回答。

1 个答案:

答案 0 :(得分:1)

如果我们需要exclude行为&#39; f2&#39;,&#39; f3&#39;和&#39; f4&#39;,只需rowSums创建逻辑vectorsubset数据集

subset(df, rowSums(df[2:4]!=0) != 0)

更新

根据OP的帖子中的更新

df[!rowSums(df[2:4] != "yes"),] 
相关问题