Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete rows with values less than in R [duplicate]

Tags:

dataframe

r

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

like image 666
sbradbio Avatar asked Dec 23 '22 15:12

sbradbio


2 Answers

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
like image 181
akrun Avatar answered Jan 12 '23 21:01

akrun


apply version

df[apply(df, 1, function(x) sum(x < 15) <= 2) ,]
like image 28
cdeterman Avatar answered Jan 12 '23 20:01

cdeterman