gtsummary

is it possible to have an overall column when gtsummary's tbl_strata?


I would like to have an overall column in this stratified table. Is it possible using tbl_strata oder do I need to make two tables and merge them?

trial |>
    select(age, grade, stage, trt) |>
    mutate(grade = paste("Grade", grade)) |>
    tbl_strata(
        strata = grade,
        .tbl_fun =
            ~ .x |>
            tbl_summary(by = trt, missing = "no"),
        .header = "**{strata}**, N = {n}"
    )

Solution

  • Sure, you create the stratified table as above, then merge it with an unstratified table. Example below!

    library(gtsummary)
    
    # create a table by Grade
    tbl_by_grade <- trial |>
      select(age, grade, stage, trt) |>
      mutate(grade = paste("Grade", grade)) |>
      tbl_strata(
        strata = grade,
        .tbl_fun =
          ~ .x |>
          tbl_summary(by = trt, include = stage),
        .header = "**{strata}**, N = {n}"
      )
    
    # create a table using all grades
    tbl_all_grades <-
      trial |>
      tbl_summary(by = trt, include = stage) |> 
      modify_spanning_header(all_stat_cols() ~ "**All Grades**, N = {N}")
    
    # merge the two tables together
    tbl_final <- list(tbl_by_grade, tbl_all_grades) |> 
      tbl_merge(tab_spanner = FALSE)
    

    enter image description here Created on 2025-01-22 with reprex v2.1.1