gpt4 book ai didi

删除数据集中的行出错

转载 作者:行者123 更新时间:2023-12-04 10:33:38 25 4
gpt4 key购买 nike

我有以下数据集:

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 中的 c() 是多余的。 which 无论如何都会返回一个向量。请永远不要给您的数据框或向量起一个同时也是函数名称的名称。您有时会遇到麻烦。

关于删除数据集中的行出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6135568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com