Sj Bq
Sj Bq

Reputation: 211

how to write result of t.test into a file?

How can i write the result of t.test into a file?

> x  
[1] 12.2 10.8 12.0 11.8 11.9 12.4 11.3 12.2 12.0 12.3  
> t.test(x)

One Sample t-test  

data:  x   
t = 76.2395, df = 9, p-value = 5.814e-14  
alternative hypothesis: true mean is not equal to 0   
95 percent confidence interval:  
 11.5372 12.2428   
sample estimates:  
mean of x   
11.89   

> write(t.test(x),file="test")    
Error in cat(list(...), file, sep, fill, labels, append) : 
argument 1 (type 'list') cannot be handled by 'cat'

Upvotes: 0

Views: 3784

Answers (2)

ravic_
ravic_

Reputation: 1831

The broom package was released after this post. It makes dealing with model outputs much, much nicer. In particular, the function tidy() will convert the model output to a dataframe for further handling.

x <- c(12.2, 10.8, 12.0, 11.8, 11.9, 12.4, 11.3, 12.2, 12.0, 12.3)
t_test <- t.test(x)

library(broom)
tidy_t_test <- broom::tidy(t_test)

tidy_t_test
#> # A tibble: 1 x 8
#>   estimate statistic  p.value parameter conf.low conf.high method
#>      <dbl>     <dbl>    <dbl>     <dbl>    <dbl>     <dbl> <chr> 
#> 1     11.9      76.2 5.81e-14         9     11.5      12.2 One S…
#> # … with 1 more variable: alternative <chr>

write.csv(tidy_t_test, "out.csv")

Upvotes: 2

IRTFM
IRTFM

Reputation: 263301

> sink("out.txt")
> x  <- scan()
1:  12.2 10.8 12.0 11.8 11.9 12.4 11.3 12.2 12.0 12.3 
11: 
Read 10 items
> t.test(x)
> sink()
> readLines("out.txt")
 [1] ""                                                    
 [2] "\tOne Sample t-test"                                 
 [3] ""                                                    
 [4] "data:  x "                                           
 [5] "t = 76.2395, df = 9, p-value = 5.814e-14"            
 [6] "alternative hypothesis: true mean is not equal to 0 "
 [7] "95 percent confidence interval:"                     
 [8] " 11.5372 12.2428 "                                   
 [9] "sample estimates:"                                   
[10] "mean of x "                                          
[11] "    11.89 "                                          
[12] ""            

Upvotes: 5

Related Questions