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

admin2025-04-21  2

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}"
    )

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}"
    )
Share Improve this question asked Jan 22 at 20:53 ebayebay 1431 silver badge13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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)

Created on 2025-01-22 with reprex v2.1.1

转载请注明原文地址:http://anycun.com/QandA/1745228732a90501.html