gpt4 book ai didi

r - 将方差分析结果写入文本文件

转载 作者:行者123 更新时间:2023-12-01 11:20:11 32 4
gpt4 key购买 nike

我尝试将 Anova 结果写入 txt。但是,我发现 write 函数无法处理任务。

我的代码如下:

##first I calculate the Anova of two variable and then stored the result in "a".
functionanova<-function(x,y,datasource){
result= aov(y ~ x, data= datasource)
return(summary(result))
}

a<-functionanova(df1$Pe,df1$Pb,df1)

##The result is:
a
Df Sum Sq Mean Sq F value Pr(>F)
x 1 2.77 2.773 0.662 0.419
Residuals 68 284.85 4.189

> class(a)
[1] "summary.aov" "listof"

##Then I try to write it to an text. However,there is an error with this type of data.
> write(a, "d:\\week1\\MYOUTFILE.txt", append= TRUE,sep = " ")

Error in cat(list(...), file, sep, fill, labels, append) : 
argument 1 (type 'list') cannot be handled by 'cat'

##I try to convert it to a data frame. However, there is also an error with it.
> aa<-as.data.frame(a)

Error in as.data.frame.default(a) : 
cannot coerce class "c("summary.aov", "listof")" to a data.frame

最佳答案

使用 capture.output :

a<-functionanova(df1$Pe,df1$Pb,df1)
capture_a <- summary(a)
capture.output(capture_a, file = "anova results.txt")

关于r - 将方差分析结果写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44760710/

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