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

Remove Columns where both rows have a non-zero number?

How would I remove columns from a data frame when both rows for that column have non-zero values.

For example I want to change the following table from the following

Dogs Cats Snakes Elephants
1 0 1 3
2 1 0 2

to the following

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

Cats Snakes
0 1
1 0

The reason the other columns are removed is because both rows had non-zero numbers. If one of the two rows has a zero then we’d retain the entire column. It does not matter which one contains the zero.

I tried to use dyplr and if else statements but most of those are based on single conditions in the column being met.

>Solution :

You may use colSums here:

df[, colSums(df!=0) != nrow(df)]

  Cats Snakes
1    0      1
2    1      0

The logic here is to retain any column such that the count of row values not equal to zero does not equal the total number of rows. Put another way, this says to retain any column having at least one zero row.

Data:

df <- data.frame(Dogs=c(1,2), Cats=c(0,1), Snakes=c(1,0), Elephants=c(3,2))
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