Home > other >  tbl_summary: How to set t.test column to 2 decimal places (gtsummary)
tbl_summary: How to set t.test column to 2 decimal places (gtsummary)

Time:01-22

I'm pretty new to tbl_summary and am trying to set the t.test column to 2 decimal places.

I've tried several different things in the tbl_summary() line but it doesnt seem to change the t.test column. Tried adding in style_fun = everything() ~ purrr::partial(style_number, digits = 2) but that led to the following error message: ... must be empty. ✖ Problematic argument: • style_fun = everything() ~ purrr::partial(style_number, digits = 2). Any ideas on how to change it?

Also, is it possible to change the CI column so that it reads as conf.low - conf.high instead of conf.low, conf.high. Thank you!

I've used a mtcars example here to make it easier.

car <- mtcars[,c("am", "mpg", "qsec", "disp")]

car %>%
  dplyr::select(am, mpg, qsec, disp) %>%
  tbl_summary(
    by="am",
    statistic = list(all_continuous() ~ "{mean} (±{sd})"),
    digits = all_continuous() ~ 2
  ) %>%
  add_p(test = all_continuous() ~ "t.test") %>%
  modify_header(statistic ~ "**Test Statistic**") %>%
  modify_fmt_fun(statistic ~ style_number) %>%
  add_ci()

CodePudding user response:

You were right about using modify_fmt_fun, but style_number needs to be wrapped in a function.

library(gtsummary)

car <- mtcars[,c("am", "mpg", "qsec", "disp")]

car %>%
  dplyr::select(am, mpg, qsec, disp) %>%
  tbl_summary(
    by="am",
    statistic = list(all_continuous() ~ "{mean} (±{sd})"),
    digits = all_continuous() ~ 2
  ) %>%
  add_p(test = all_continuous() ~ "t.test") %>%
  modify_header(statistic ~ "**Test Statistic**") %>%
  modify_fmt_fun(update = statistic ~ function(x) style_number(x, digits = 2)) %>%
  add_ci() 

enter image description here

  • Related