Home > Mobile >  What is the quickest way to verify whether the first 10 elements of a vector are non-zero in R?
What is the quickest way to verify whether the first 10 elements of a vector are non-zero in R?

Time:12-08

Suppose we have a 300 x 1000 matrix. If there exists any row of this matrix such that the first 10 elements of the row are non-zero, then a condition is satisfied. What is the quickest way to write this code instead of doing it using for loops?

CodePudding user response:

any(apply(m[, 1:10], MARGIN = 1, function(x) all(x != 0))) should be pretty quick.

  •  Tags:  
  • r
  • Related