beginner
beginner

Reputation: 411

Select rows from data frame which have at least three negative values?

How to filter rows which have atleast 3 negative values?

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

expected output

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

i tried the code given below, but it doesn't work

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

Upvotes: 0

Views: 91

Answers (1)

David Heckmann
David Heckmann

Reputation: 2939

try:

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

Upvotes: 2

Related Questions