How to delete rows containing "0" specific columns while keeping the row identifiers of the remaining rows in R

I'm trying to figure it out, but could not do it. I want to get rid of all lines with values ​​of "0", but not saving the identification numbers of the remaining lines.

ID  B   C   D
1_2 34  42  12
1_3 34  32  2
1_4 0   0   0
1_5 12  33  12

the conclusion should be

ID  B   C   D
1_2 34  42  12
1_3 34  32  2
1_5 12  33  12
+5
source share
2 answers

if you want to delete rows containing 0 or a lot for columns B, C or D:

DF[apply(DF[c(2:4)],1,function(z) !any(z==0)),] 

or only when all columns B, C, D contain 0:

DF[apply(DF[c(2:4)],1,function(z) any(z!=0)),]
+5
source

If tmp is the name of the data.frame source file, then the following works:

tmp2 <- data.frame(Reduce(rbind,apply(tmp,1,function(x){if(any(x==0)){NULL}else{x}})))
+1
source

All Articles