Search code examples
gtsummary

Trying to display both mean and median with gtsummary


What is the correct syntax to display both median and mean of a continuous variable using tbl_continuous? Also, is it possible to display on 2 lines as you can do with tbl_summary and the continuous2 argument?

Code below is just displaying medians (see image).

comparison.data %>%
  select(imaging, los.minutes, acuity) %>%
  tbl_continuous(
    by = imaging,
    variable = los.minutes,
    statistic = 
     los.minutes ~ c("{mean} ({sd})", 
                     "{median} ({sd})")
  ) %>%
  modify_spanning_header(all_stat_cols() ~ "**Imaging status**")

Just displaying medians


Solution

  • Example below!

    library(gtsummary)
    packageVersion("gtsummary")
    #> [1] '1.6.1'
    
    tbl <-
      trial %>%
      tbl_continuous(
        variable = age, 
        by = trt,
        include = grade,
        statistic = ~"{mean}  \n{median}"
      ) %>% 
      as_gt() %>% 
      gt::fmt_markdown(columns = everything())
    

    enter image description here Created on 2022-08-24 by the reprex package (v2.0.1)