0

I want to delete the rows that are having values which are all zeros. This is how my data set looks like:

ChildBks YouthBks CookBks DoItYBks RefBks ArtBks
    1       0        1       0       0      0
    0       0        0       0       0      0
    1       1        0       0       0      1
    0       0        1       1       0      0 

I want to have it like this

ChildBks YouthBks CookBks DoItYBks RefBks ArtBks
    1       0        1       0       0      0
    1       1        0       0       0      1
    0       0        1       1       0      0 

How can we delete the second row as all the values in this rows are equal to zero.

Thanks.

Cettt
  • 11,460
  • 7
  • 35
  • 58

1 Answers1

0

if your data.frame is df then you can use

df[!apply(df, 1, function(x) all(x == 0)), ]

or equivalently you could use

df[apply(df, 1, function(x) any(x != 0)), ]

Note that the solution suggested by Sotos in the comments is valid only if you have no negative values in your columns. If this is the case I think his solution is faster.

Cettt
  • 11,460
  • 7
  • 35
  • 58