删除具有负值的行

发布于 2024-10-03 07:09:46 字数 438 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

药祭#氼 2024-10-10 07:09:46

马雷克是对的,这是一个数据问题。现在,如果您使用 [as.numeric(ants$Turbidity] ,请小心,因为该值始终为正。它给出了因子级别(1 到 length(ants$Turbidity)),而不是数字因子。

试试这个:

tt <- as.numeric(as.character(ants$Turbidity))
which(!is.na(tt))

它将为您提供一个值首先不是数字的索引列表。这应该使您能够首先清理数据,

例如:

> Turbidity <- factor(c(1,2,3,4,5,6,7,8,9,0,"a"))
> tt <- as.numeric(as.character(Turbidity))
Warning message:
NAs introduced by coercion 
> which(is.na(tt))
[1] 11

您不应该使用 as.numeric(as.character(... )) 结构来转换有问题的数据,因为它会生成 NA,从而扰乱其余数据,例如:

> Turbidity[tt > 5]
[1] 6    7    8    9    <NA>
Levels: 0 1 2 3 4 5 6 7 8 9 a

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 :

tt <- as.numeric(as.character(ants$Turbidity))
which(!is.na(tt))

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:

> Turbidity <- factor(c(1,2,3,4,5,6,7,8,9,0,"a"))
> tt <- as.numeric(as.character(Turbidity))
Warning message:
NAs introduced by coercion 
> which(is.na(tt))
[1] 11

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:

> Turbidity[tt > 5]
[1] 6    7    8    9    <NA>
Levels: 0 1 2 3 4 5 6 7 8 9 a
夕色琉璃 2024-10-10 07:09:46

读入数据后一定要进行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.

〃温暖了心ぐ 2024-10-10 07:09:46

编辑。我忘记了 as.character 转换(请参阅 Joris 评论)。


消息意味着 ants$Turbidit 是一个因素。 时,它就会起作用。

ants <- ants[as.numeric(as.character(ants$Turbidity)) > 0,]

当您执行或

ants <- subset(ants, as.character(as.numeric(Turbidity)) > 0)

但真正的问题是您的数据还没有准备好进行分析。这种转换应该在一开始就完成。您应该小心,因为也可能存在非数字值。

EDIT. I forget about as.character conversion (see Joris comment).


Message mean that ants$Turbidit is a factor. It will work when you do

ants <- ants[as.numeric(as.character(ants$Turbidity)) > 0,]

or

ants <- subset(ants, as.character(as.numeric(Turbidity)) > 0)

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.

孤城病女 2024-10-10 07:09:46

这也应该使用 tidyverse 工作(假设列是正确的数据类型)。

ants %>% dplyr::filter(Turbidity >= 0)

This should also work using the tidyverse (assuming column is the correct data type).

ants %>% dplyr::filter(Turbidity >= 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文