giegie
giegie

Reputation: 463

Filter out columns from dataframe in R

I would like to filter out all columns where the value in row "b" is >2% of the sum of all rows in a given column.

df
   x   y    z
a  99  95  99
b  1   4    1
c  0   1    0
result
   x   z  
a  99  99
b  1   1
c  0   0

Upvotes: 0

Views: 71

Answers (1)

zx8754
zx8754

Reputation: 56054

Try this:

df1[, df1["b", ]/colSums(df1) <= 0.02 ]
#    x  z
# a 99 99
# b  1  1
# c  0  0

Upvotes: 3

Related Questions