3

我有以下数据集:

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)),]
4

3 回答 3

3

原因是没有零时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,]

在旁注中,c()您的 oneliner 中的 是多余的。which无论如何返回一个向量。并且请永远不要给你的数据框或向量一个名字,这也是一个函数的名字。你会在某一时刻遇到麻烦。

于 2011-05-26T08:23:35.300 回答
2

这是另一种方法。

data[!data$is.numeric.feature == 0, ]
于 2011-05-26T08:33:10.440 回答
0

出错是因为该which语句返回 integer(0),一个空的整数向量。索引-numeric(0)不被解释为“不要省略任何东西”,而是作为索引integer(0),这意味着“没有索引”。我认为如果您的数据中至少有一个零,它应该是正确的。

但是无论如何您都不需要,并且逻辑向量很好。这些都有效:

data[data$is.numeric.feature!=0,]

subset(data,is.numeric.feature!=0)
于 2011-05-26T08:24:31.613 回答