R 统计:简单列向量的问题 [英] R statistics: problem with simple column vector
问题描述
我在使用使用 read.delim
导入的制表符分隔数据文件中的数据时遇到问题.
I have a problem using data from a tab delimited data file imported with read.delim
.
大多数列包含我需要对其进行 t.test
的数字数据.不幸的是,我总是收到此错误:
Most of the columns contain numerical data which I need to do a t.test
for. Unfortunately I always get this error:
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
我注意到这只发生在由不同级别组成的向量中.它甚至不会执行像 vector[1] + vector[2] 这样的简单数值运算来处理水平向量.不过,没有级别的矢量工作正常.
I noticed that this only happens with vectors that consist of different levels. It won't even perform simple numerical operations like vector[1] + vector[2] for leveled vectors. Vectors without levels work fine, though.
如何使用水平向量中的数据进行计算?
How can I use the data in the leveled vectors for calculation?
谢谢
推荐答案
我已经能够通过以下小示例重现您的错误消息:
I have been able to reproduce your error message with the following small example:
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 检验.加法同样没有为因子定义:
The problem is you are trying to perform a t-test on non-numeric vectors. Addition likewise is not defined for factors:
x + y
收益
[1] NA NA NA NA NA
Warning message:
In Ops.factor(x, y) : + not meaningful for factors
该警告提供了对错误的敏锐洞察,并解释了为什么您的 t 检验不起作用.
The warning gives keen insight as to what is amiss and also explains why your t-test is not working.
要解决这个问题,您需要按照 ilya 的建议进行操作:使用 as.numeric(as.character())
To fix the problem, you need to do as ilya suggests: convert your vectors to numeric with as.numeric(as.character())
这篇关于R 统计:简单列向量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!