rbroomrstatix

make nice simple t-test (difference in means) table that can be exported to latex in R


The biggest problem with using methods such as through library(broom)

library(dplyr)
library(broom)

output <- iris %>% 
  group_by(Species) %>% 
  summarise(tidy(t.test(Sepal.Width)))

Is that it produces a tibble which is essentially a glorified dataframe. I do not want a dataframe, but would prefer a summary-stats table. One method I found was to convert it to a regular dataframe and then add row names, and then use xtable to export it to latex. There is no good code for this available as far as I am aware and doing this is just inefficient in my opinion.

Another method is to use https://www.datanovia.com/en/blog/how-to-perform-t-test-for-multiple-variables-in-r-pairwise-group-comparisons/ but again, we end up with a tibble which is not ideal for exporting it to latex as I'd basically have to reformat all labels anyway.

Any other suggestions?


Solution

  • The flextable package allows you to easily embed tables into latex files. Since you are comparing three groups, you really should be applying corrections like Tukey's HSD since this would be more of an ANOVA comparison anyway. I have included the code for that instead, however if you are set on the t-test method you can use the same flextable coding for that purpose. First, you can get a very bare bones table like so:

    #### Tukey HSD Version ####
    tukey.output <- iris %>% 
      rstatix::tukey_hsd(Sepal.Width ~ Species)
    
    tukey.output %>% 
      flextable()
    

    However you'll notice it looks fairly sloppy for this specific case because the p values have many decimals and the names are kinda crap:

    enter image description here

    We can prettify it a bit by adding a fair amount of code. Keep in mind I added some spaces in some of the names because flextable will try to fit the table as best it can into the limited width of the page, so it can sometimes smoosh things together.

    tukey.output %>% 
      select(-term,
             -null.value) %>% 
      rename(`Species 1` = group1,
             `Species 2` = group2,
             `Estimate` = estimate,
             `CI Lower` = conf.low,
             `CI Upper ` = conf.high,
             ` P Value` = p.adj,
             `P Sig?` = p.adj.signif) %>% 
      mutate(`Species 1` = c("Setosa",
                             "Setosa",
                             "Versicolor"),
             `Species 2` = c("Versicolor",
                             "Virginica",
                             "Virginica"),
             `CI Lower` = round(`CI Lower`,3),
             `CI Upper ` = round(`CI Upper `,3),
             ` P Value` = c(" < 0.00",
                           " < 0.00",
                           " < 0.00")) %>% 
      flextable() %>% 
      add_header_lines(values = "Tukey HSD Table for Iris Dataset")
    

    Which gives you this:

    enter image description here