我是R的新手并试图做一个简单的方差分析。我创建了一个数据框:
MeanTable <- data.frame(Age=c("2","2","2","4","4","4","6","6","6"),
Yield=c("12.812","15.17","13.868","24.456","23.444","25.312",
"21.146","21.63","21.84"),stringsAsFactors = FALSE)
然后尝试进行ANOVA,但我收到了这条警告信息:
警告讯息: 1:在mean.default(X [[i]],...)中: 参数不是数字或逻辑:返回NA 2:在mean.default(X [[i]],...)中: 参数不是数字或逻辑:返回NA 3:在mean.default(X [[i]],...)中: 参数不是数字或逻辑:返回NA
所以我认为这意味着我打算使用as.numeric将值从因子转换为数字。所以我这样做了:
as.numeric(MeanTable$Age)
is.numeric(MeanTable$Age)
但得到了FALSE
。我究竟做错了什么?或者我的问题完全是另一回事?
答案 0 :(得分:2)
您需要将响应值更改为数据框中的数字。
MeanTable <- data.frame(Age=c("2","2","2","4","4","4","6","6","6"),
Yield=c("12.812","15.17","13.868","24.456","23.444","25.312",
"21.146","21.63","21.84"),stringsAsFactors = FALSE)
MeanTable$Yield <- as.numeric(MeanTable$Yield)
lm(Yield~Age,data=MeanTable)
正如@rawr指出的那样,如果您的变量以数字开头会更容易。但是,要小心:如果你使Age
数字而不是分类,那么你将拟合回归模型而不是单向方差分析。