gpt4 book ai didi

R统计: problem with simple column vector

转载 作者:行者123 更新时间:2023-12-02 02:53:28 25 4
gpt4 key购买 nike

我在使用通过 read.delim 导入的制表符分隔数据文件中的数据时遇到问题。

大多数列都包含我需要对其进行 t.test 的数值数据。不幸的是我总是收到这个错误:

Error in if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) 
stop("data are essentiallyconstant") :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In mean.default(x) : argument is not numeric or logical: returning NA
2: In mean.default(y) : argument is not numeric or logical: returning NA

我注意到这只发生在由不同级别组成的向量上。它甚至不会执行简单的数值运算,例如对于分级向量的向量[1] +向量[2]。不过,没有级别的向量可以正常工作。

如何使用分级向量中的数据进行计算?

谢谢

最佳答案

我已经能够通过以下小示例重现您的错误消息:

x = as.factor(1:5)
y = as.factor(1:5)

t.test(x, y)

产量

Error in if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) stop("data are essentially constant") : 
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In mean.default(x) : argument is not numeric or logical: returning NA
2: In mean.default(y) : argument is not numeric or logical: returning NA

问题是您正在尝试对非数字向量执行 t 检验。同样,加法也没有为因子定义:

x + y

产量

[1] NA NA NA NA NA
Warning message:
In Ops.factor(x, y) : + not meaningful for factors

该警告提供了关于问题所在的敏锐见解,并解释了为什么您的 t 检验不起作用。

要解决此问题,您需要按照 ilya 的建议进行操作:使用 as.numeric(as.character()) 将向量转换为数字

关于R统计: problem with simple column vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3247178/

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