[英]in R - How can I remove specific columns in data.frame?
In this data.frame: 在此data.frame中:
I want to remove some columns that "Total" == 0
我想删除
"Total" == 0
某些列
I reviewed some questions about my problems in here and I found it 我在这里回顾了一些有关我的问题的问题,然后找到了
R: how to remove certain rows in data.frame R:如何删除data.frame中的某些行
but I can't apply perfectly in my case. 但我不能完美地适用于我的情况。
Assuming that the "Total" in the "x' column denotes the last row (I have no reason to think otherwise), we subset the last row ( nrow(df1)
) and remove the first column (as it is non-numeric), check whether that is not equal to 0. We get a logical vector
from that. Concatenate ( c
) with TRUE
so that we can also get the first column ie "x" in the subset dataset. 假定“ x”列中的“总计”表示最后一行(我没有其他理由认为),我们将最后一行(
nrow(df1)
)作为子集并删除第一列(因为它是非数字) ,检查是否等于0,从中得到一个逻辑vector
,将( c
)与TRUE
以便我们还可以获得子集数据集中的第一列,即“ x”。
df1[c(TRUE,df1[nrow(df1),-1]!=0)]
# x v2 v3
#1 a 1 0
#2 b 3 1
#3 Total 4 1
df1 <- data.frame(x= c('a', 'b', 'Total'),
v1= c(0,0,0), v2= c(1,3,4), v3=c(0, 1,1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.