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 > 0 produces matrix values true element > 0
  • function rowsums returns number of nonzero elements every line (when summing treats values true 1 , false 0)
  • finally [ selects lines number of non-zero elements >= 5

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -