Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

P.s. please ignore (or correct) the tags of this post. I just was’t sure which tags would be most appropriate for this problem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading