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

Delete specific columns

I have

F <- structure(c(0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0), .Dim = c(3L, 
5L))

How can I remove from F the columns that have less than 2 consecutive zero?
Thx!

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 :

We may use rle to determine the consecutive values i.e. 0 and create a logical condition with lengths by looping over the column (apply, MARGIN = 2)

F[,!apply(F, 2, function(x) with(rle(!x), 
      any(lengths >= 2 & values))), drop = FALSE]

-output

      [,1] [,2]
[1,]    0    0
[2,]    1    1
[3,]    1    1

If it is the opposite, just remove the !

F[,apply(F, 2, function(x) with(rle(!x), 
      any(lengths >= 2 & values))), drop = FALSE]
      [,1] [,2] [,3]
[1,]    1    1    1
[2,]    0    0    0
[3,]    0    0    0
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