I am trying to sort it out but could not able to do it. I want to get rid of all the rows with '0' values but keeping the ID numbers intact of remaining rows.
ID B C D
1_2 34 42 12
1_3 34 32 2
1_4 0 0 0
1_5 12 33 12
output should be
ID B C D
1_2 34 42 12
1_3 34 32 2
1_5 12 33 12
if you want to remove the lines containing a 0 or many for column B,C or D :
DF[apply(DF[c(2:4)],1,function(z) !any(z==0)),]
or only when all columns B,C,D contains 0 :
DF[apply(DF[c(2:4)],1,function(z) any(z!=0)),]
If tmp is the name of your original data.frame, the following works:
tmp2 <- data.frame(Reduce(rbind,apply(tmp,1,function(x){if(any(x==0)){NULL}else{x}})))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With