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
如何在data.frame中删除某些行
but I can't apply perfectly in my case.
但我不能完全适用于我的情况。
1 个解决方案
#1
0
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 i.e. "x" in the subset dataset.
假设“x”列中的“Total”表示最后一行(我没有理由不这么认为),我们将最后一行(nrow(df1))进行子集划分,并删除第一列(因为它不是数值),检查它是否不等于0。我们从中得到一个逻辑向量。用TRUE连接(c)这样我们就可以得到第一列也就是。在子集数据集中的“x”。
df1[c(TRUE,df1[nrow(df1),-1]!=0)]
# x v2 v3
#1 a 1 0
#2 b 3 1
#3 Total 4 1
data
df1 <- data.frame(x= c('a', 'b', 'Total'),
v1= c(0,0,0), v2= c(1,3,4), v3=c(0, 1,1))
#1
0
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 i.e. "x" in the subset dataset.
假设“x”列中的“Total”表示最后一行(我没有理由不这么认为),我们将最后一行(nrow(df1))进行子集划分,并删除第一列(因为它不是数值),检查它是否不等于0。我们从中得到一个逻辑向量。用TRUE连接(c)这样我们就可以得到第一列也就是。在子集数据集中的“x”。
df1[c(TRUE,df1[nrow(df1),-1]!=0)]
# x v2 v3
#1 a 1 0
#2 b 3 1
#3 Total 4 1
data
df1 <- data.frame(x= c('a', 'b', 'Total'),
v1= c(0,0,0), v2= c(1,3,4), v3=c(0, 1,1))