dataframe - R: Remove rows with fewer than certain threshold non-zero values -
i know how remove rows data frame have fewer (let's 5) non-zero entries.
the closest i've come is:
length(which(df[1,] > 0)) >= 5 but how apply whole data frame , drop ones false? there function similar countif() function in excel can apply here?
thank help.
you can use boolean values in rowsums , in [:
df[ rowsums(df > 0) >= 5, ] there 3 steps hidden in expression:
- expression
df > 0produces matrix values true element > 0 - function
rowsumsreturns number of nonzero elements every line (when summing treats values true 1 , false 0) - finally
[selects lines number of non-zero elements >= 5
Comments
Post a Comment