0

I was provided with three t-tests:

Two Sample t-test

data:  cammol by gender
t = -3.8406, df = 175, p-value = 0.0001714
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.11460843 -0.03680225
sample estimates:
mean in group 1 mean in group 2 
       2.318132        2.393837 


Welch Two Sample t-test

data:  alkphos by gender
t = -2.9613, df = 145.68, p-value = 0.003578
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -22.351819  -4.458589
sample estimates:
mean in group 1 mean in group 2 
       85.81319        99.21839 


Two Sample t-test

data:  phosmol by gender
t = -3.4522, df = 175, p-value = 0.0006971
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.14029556 -0.03823242
sample estimates:
mean in group 1 mean in group 2 
       1.059341        1.148605

And I want to construct a table with these t-test results in R markdown like: wanted_table_format

I've tried reading some instructions for using "knitr" and "kable" functions, but honestly, I do not know how to apply the t-test results to those functions.

What could I do?

jhlee
  • 1
  • 1
  • 1
  • 2
  • 1
    Try with `broom::tidy` – akrun May 19 '17 at 10:25
  • I'm a newbie to this R language, so please don't go mad if I say something dumb: – jhlee May 19 '17 at 10:31
  • 1
    I noticed that broom and tidy weren't installed on my R, so I tried doing install.packages(broom), but the program says: "object broom not found" – jhlee May 19 '17 at 10:32
  • 2
    You need `install.packages("broom")` with quotes – akrun May 19 '17 at 10:32
  • 1
    What do you expect the result to look like? Which statistics do you wish to output? You can see how to access them using `str(x)` where `x` is your object. This will tell you the structure and names of elements (such as `x$conf.int`, `x$statistic`, `x$parameter`, `x$p.value`, `x$estimate`). – Roman Luštrik May 19 '17 at 10:40
  • Uh, silly mistake... in order to make a table, I first have to take the t-test above as an input.. right..? like ( test <- "something" ) If I have to, how can I take those t-tests as inputs? – jhlee May 19 '17 at 10:41
  • Or is there a manual way to construct a table? – jhlee May 19 '17 at 10:46

1 Answers1

4

Suppose your three t-tests are saved as t1, t2, and t3.

t1 <- t.test(rnorm(100), rnorm(100)
t2 <- t.test(rnorm(100), rnorm(100, 1))
t3 <- t.test(rnorm(100), rnorm(100, 2))

You could turn them into one data frame (that can then be printed as a table) with the broom and purrr packages:

library(broom)
library(purrr)

tab <- map_df(list(t1, t2, t3), tidy)

On the above data, this would become:

     estimate    estimate1   estimate2  statistic      p.value parameter   conf.low  conf.high
1  0.07889713 -0.008136139 -0.08703327   0.535986 5.925840e-01  193.4152 -0.2114261  0.3692204
2 -0.84980010  0.132836627  0.98263673  -6.169076 3.913068e-09  194.2561 -1.1214809 -0.5781193
3 -1.95876967 -0.039048940  1.91972073 -13.270232 3.618929e-29  197.9963 -2.2498519 -1.6676875
                   method alternative
1 Welch Two Sample t-test   two.sided
2 Welch Two Sample t-test   two.sided
3 Welch Two Sample t-test   two.sided

Some of the columns probably don't matter to you, so you could do something like this to get just the columns you want:

tab[c("estimate", "statistic", "p.value", "conf.low", "conf.high")]

As noted in the comments, you'd have to first do install.packages("broom") and install.packages("purrr").

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • 2
    is there a way to add a column showing which t-test the output came from t1, t2 or t3? Also how do you reduce the number of sig figs show in the table? – I Del Toro May 20 '20 at 13:45