删除具有负值的行
在 RI 中,我尝试删除数据框(蚂蚁)中列标题“浊度”下具有负值的行。我已经尝试过
ants<-ants[ants$Turbidity<0,]
,但它返回以下错误:
Warning message:
In Ops.factor(ants$Turbidity, 0) : < not meaningful for factors
有什么想法可能是这样吗?也许我需要设置负值 NA 然后我删除所有 NA 吗?
任何想法非常感谢,谢谢!
@Joris:结果是
str(ants$Turbidity)
num [1:291] 0 0 -0.1 -0.2 -0.2 -0.5 0.1 -0.4 0 -0.2 ...
In R I am trying to delete rows within a dataframe (ants) which have a negative value under the column heading Turbidity. I have tried
ants<-ants[ants$Turbidity<0,]
but it returns the following error:
Warning message:
In Ops.factor(ants$Turbidity, 0) : < not meaningful for factors
Any ideas why this may be? Perhaps I need to make the negative values
NA before I then delete all NAs?
Any ideas much appreciated, thank you!
@Joris: result is
str(ants$Turbidity)
num [1:291] 0 0 -0.1 -0.2 -0.2 -0.5 0.1 -0.4 0 -0.2 ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
马雷克是对的,这是一个数据问题。现在,如果您使用 [as.numeric(ants$Turbidity] ,请小心,因为该值始终为正。它给出了因子级别(1 到 length(ants$Turbidity)),而不是数字因子。
试试这个:
它将为您提供一个值首先不是数字的索引列表。这应该使您能够首先清理数据,
例如:
您不应该使用
as.numeric(as.character(... ))
结构来转换有问题的数据,因为它会生成 NA,从而扰乱其余数据,例如:Marek is right, it's a data problem. Now be careful if you use [as.numeric(ants$Turbidity] , as that one will always be positive. It gives the factor levels (1 to length(ants$Turbidity)), not the numeric factors.
Try this :
It will give you a list of indices where the value was not numeric in the first place. This should enable you to first clean up your data.
eg:
You shouldn't use the
as.numeric(as.character(...))
structure to convert problematic data, as it will generate NA's that will mess with the rest. Eg:读入数据后一定要进行
summary(ants)
,并检查是否得到了预期的结果。它会为你省去很多问题。数字数据很容易神奇地转换为字符或因子类型。
Always do
summary(ants)
after reading in data, and check if you get what you expect.It will save you lots of problems. Numeric data is prone to magic conversion to character or factor types.
编辑。我忘记了
as.character
转换(请参阅 Joris 评论)。消息意味着
ants$Turbidit
是一个因素
。 时,它就会起作用。当您执行或
但真正的问题是您的数据还没有准备好进行分析。这种转换应该在一开始就完成。您应该小心,因为也可能存在非数字值。
EDIT. I forget about
as.character
conversion (see Joris comment).Message mean that
ants$Turbidit
is afactor
. It will work when you door
But the real problem is that your data are not prepared to analysis. Such conversion should be done in the beginning. You should be careful cause there could be non-numeric values also.
这也应该使用 tidyverse 工作(假设列是正确的数据类型)。
This should also work using the tidyverse (assuming column is the correct data type).