I have a df in R
    sample1 sample2 sample3 sample4
price1  10  21  32  43
price2  12  24  15  18
price3  1   2   15  8
price4  16  30  44  58
price5  18  33  48  63
price6  20  36  52  68
price7  22  39  56  73
price8  24  42  60  78
price9  26  45  64  83
price10 28  48  68  88
I would like to perform an operations where i would like to drop rows that have values less than 15 in more than 2 columns
so
price3  1   2   15  8
would be deleted
We can use rowSums to create the logical vector
df1[rowSums(df1 < 15) <=2 , , drop = FALSE]
#        sample1 sample2 sample3 sample4
#price1       10      21      32      43
#price2       12      24      15      18
#price4       16      30      44      58
#price5       18      33      48      63
#price6       20      36      52      68
#price7       22      39      56      73
#price8       24      42      60      78
#price9       26      45      64      83
#price10      28      48      68      88
                        apply version
df[apply(df, 1, function(x) sum(x < 15) <= 2) ,]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With