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.
我试图解决它,但无法做到这一点。我想摆脱所有'0'值的行,但保持剩余行的ID号完好无损。
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
2 个解决方案
#1
5
if you want to remove the lines containing a 0 or many for column B,C or D :
如果要删除列B,C或D中包含0或多行的行:
DF[apply(DF[c(2:4)],1,function(z) !any(z==0)),]
or only when all columns B,C,D contains 0 :
或仅当所有列B,C,D包含0时:
DF[apply(DF[c(2:4)],1,function(z) any(z!=0)),]
#2
1
If tmp is the name of your original data.frame, the following works:
如果tmp是原始data.frame的名称,则以下工作方式:
tmp2 <- data.frame(Reduce(rbind,apply(tmp,1,function(x){if(any(x==0)){NULL}else{x}})))
#1
5
if you want to remove the lines containing a 0 or many for column B,C or D :
如果要删除列B,C或D中包含0或多行的行:
DF[apply(DF[c(2:4)],1,function(z) !any(z==0)),]
or only when all columns B,C,D contains 0 :
或仅当所有列B,C,D包含0时:
DF[apply(DF[c(2:4)],1,function(z) any(z!=0)),]
#2
1
If tmp is the name of your original data.frame, the following works:
如果tmp是原始data.frame的名称,则以下工作方式:
tmp2 <- data.frame(Reduce(rbind,apply(tmp,1,function(x){if(any(x==0)){NULL}else{x}})))