使用R中的ANOVA分析多列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用R中的ANOVA分析多列相关的知识,希望对你有一定的参考价值。

我正在使用以下代码来分析我的数据

aov.models <- lapply(setdiff(names(mtcars), "cyl"), function(s) {
  aov(as.formula(paste(s, " ~ cyl")),mtcars)
})

--但是,我不确定如何将列表输出的列表打印到文本文件(我尝试展平但收到错误:Error in as.character.factor(x) : malformed factor)

-一次只能汇总一个,例如summary(aov.models[[5]])

-此外,列表丢失了变量名,现在每个条目都只是一个数字。

答案

这是您想要的吗?

names <- list()
aov.models <- lapply(setdiff(names(mtcars), "cyl"), function(s) {
  names <<- append(names, s) # Note the global assignment
  aov(as.formula(paste(s, " ~ cyl")),mtcars)
})
names(aov.models) <- names

out <- capture.output(aov.models)
cat(out, file="myOutput.txt", sep="
")

根据以下评论中OP的要求。

[summary提供了p值,print(在演示代码中隐式调用的默认方法)没有提供。

out <- capture.output(lapply(aov.models, summary))
cat(out, file="myOutput.txt", sep="
")

以上是关于使用R中的ANOVA分析多列的主要内容,如果未能解决你的问题,请参考以下文章