'How to arrange statistics as columns in gtsummary::tbl_summary function?

I have median,IQR, mean as the statistics to compute for variables in a dataset. The default out put looks like this: enter image description here

How do I add a line in the tbl_summary function that make the summary table like below? ( this is just one of the variable showing) enter image description here

summary_table<-df %>% tbl_summary(type = all_continuous() ~ "continuous2",statistic = list(all_continuous() ~ c("{mean}({sd})","{median}", "({p25}, {p75})","{min}, {max}")),



Solution 1:[1]

You can construct a table like this by merging multiple tbl_summary() tables. Example below!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.2'

stats <- c("N" = "{length}", 
           "Mean (SD)" = "{mean} ({sd})", 
           "(IQR)" = "({p25}, {p75})", 
           "Range" = "{min}, {max}")

tbl <- 
  purrr::imap(
  stats,
  ~trial %>%
    tbl_summary(include = "age", missing = "no", statistic = ~.x)  %>%
    modify_header(all_stat_cols() ~ stringr::str_glue("**{.y}**"))
) %>%
  tbl_merge(tab_spanner = FALSE) %>%
  modify_footnote(~NA)

Created on 2022-03-29 by the reprex package (v2.0.1)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel D. Sjoberg