在 R 中使用方差分析分析多列

Analysing multiple columns with ANOVA in R

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

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="\n")

在下面的评论中响应 OP 的请求。

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

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