删除数据集中的行会出错

| 我有以下数据集:
text <- c(1:13)
numbers <- c(1,1,1,1,1,1,1,1,1,1,1,1,1)
test <- data.frame(
    text =text,
    is.numeric.feature = numbers)

   text is.numeric.feature
1     1                  1
2     2                  1
...
13    13                 1
现在,我要删除数字特征== 0的所有行(这里没有,但在其他数据集中有) 当我使用以下命令时,我的完整数据集为空,我做错了什么?
test[-c(which(test$is.numeric.feature==0)),]
    
已邀请:
原因是
which(data$is.numeric.feature==0)
在没有零的情况下返回
integer(0)
> Data[-integer(0),]
[1] text               is.numeric.feature
<0 rows> (or 0-length row.names)
为了克服这个问题,可以更好地使用逻辑向量:
Data[Data$is.numeric.feature!=0,]
在旁注中,oneliner中的“ 6”是多余的。
which
仍然返回一个向量。而且,请永远不要给您的数据框或向量一个名称,该名称也是函数的名称。您将一时遇到麻烦。     
这是执行此操作的另一种方法。
data[!data$is.numeric.feature == 0, ]
    
出错是因为
which
语句返回整数(0),这是一个空的整数向量。索引ѭ10not不会解释为“不遗漏任何内容”,而是索引为
integer(0)
表示“不索引任何内容”。我认为如果您的数据中至少有一个零,那应该正确。 但是无论如何您都不需要哪个,逻辑向量也很好。这两个工作:
data[data$is.numeric.feature!=0,]

subset(data,is.numeric.feature!=0)
    

要回复问题请先登录注册