ddsjoberg / gtsummary

Presentation-Ready Data Summary and Analytic Result Tables
http://www.danieldsjoberg.com/gtsummary
Other
1.06k stars 128 forks source link

Feature request: Output Summary Statistics in Separate Columns #972

Closed eweisbrod closed 3 years ago

eweisbrod commented 3 years ago

Sorry if this is a dumb question:

I googled around for a long time and couldn't figure it out.

Is it possible to output each summary statistic in a separate column?

I was able to output each summary statistic (e.g. {mean}, {median}, etc.) in a single column as the default style. I was also able to output each summary statistic in a separate row for each variable, using the continuous2 style.

However, what I need is similar to the default style except to have a column with heading "mean", another with heading "median", and so on.

Is there an easy way to do this?

If not, is it possible to add this? Maybe as type = "continuous3" or something?

Thanks!

ddsjoberg commented 3 years ago

It's a good question!

To do this, loop over the statistics you want to show, and merge the tables together. Example below!

library(gtsummary)
#> #BlackLivesMatter
library(tidyverse)
#> Warning: package 'readr' was built under R version 4.1.1

# define continuous summary stats
tbl <-
  c(Mean = "{mean}", Median = "{median}", 
    IQR = "{p25}, {p75}", Range = "{min}, {max}") %>%
  # create a separate summary table for each statistic
  imap(
    ~trial %>%
      select(age, marker) %>%
      tbl_summary(
        statistic = all_continuous() ~ .x,
        missing = "no"
      ) %>%
      modify_header(stat_0 ~ str_glue("**{.y}**"))
  ) %>%
  # merge all summary tables together
  tbl_merge() %>%
  # remove spanning headers and footnote
  modify_spanning_header(everything() ~ NA) %>%
  modify_footnote(everything() ~ NA)

image

Created on 2021-08-26 by the reprex package (v2.0.1)

eweisbrod commented 3 years ago

Thank you sooooooooooo much!!!!! This is exactly what I needed and I was struggling for hours! Great package!