从数据框中选择至少有三个负值的行?

时间:2016-02-23 15:40:43

标签: r

如何过滤至少有3个负值的行?

df1 <-    
    Name     flex         flex2         flex3         flex4
    Set1   -1.19045139   -1.25005615  -1.053900875   -1.15142391
    Set2   -2.22129212    1.54901305   0.003462145    1.06170243
    Set3   -2.08952248   -1.95241584  -1.206060696   -1.65450460
    Set4   -1.20091116    0.50700470  -0.098793884    1.50406054
    Set5   -1.11771409    0.01175919  -3.056481406   -1.09328262
    Set7   -9.01648659    0.07707638   0.198978232    1.18125182

预期产出

df2 <- 
        Name     flex         flex2         flex3         flex4
        Set1   -1.19045139   -1.25005615  -1.053900875   -1.15142391
        Set3   -2.08952248   -1.95241584  -1.206060696   -1.65450460
        Set5   -1.11771409    0.01175919  -3.056481406   -1.09328262

我尝试了下面给出的代码,但它没有工作

Index= as.vector(which(apply(df1,1,function(x){sum(x < -1)/length(x)})>=0.75))
df1[Index,]

1 个答案:

答案 0 :(得分:2)

尝试:

   df1[ rowSums(df1<0)>=3 , ]