Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Anova result to text file

Tags:

r

I try to write an Anova result to txt. However, I find that the write function cannot deal with the task.

My code is listed below:

##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
like image 598
Yipin Avatar asked Sep 15 '25 04:09

Yipin


1 Answers

Use capture.output:

a<-functionanova(df1$Pe,df1$Pb,df1)
capture_a <- summary(a)
capture.output(capture_a, file = "anova results.txt")
like image 142
Matt Avatar answered Sep 16 '25 19:09

Matt