rgtsummary

How to add total column in gt_summary stratified table


I am trying to create stratified summary table with 93 levels of stratification.

I used the below code

table0 <-  data1 %>%  select(A6_ULB_NAME, D6, D7, A8_TYPE_OF_HOUSE) %>% tbl_strata(strata = A6_ULB_NAME,.tbl_fun = ~ .x |> tbl_summary(by = A8_TYPE_OF_HOUSE, missing = "no") ) %>% add_overall()

But unfortunately, getting the below error

Error in UseMethod("add_overall") : no applicable method for 'add_overall' applied to an object of class "c('tbl_strata', 'tbl_merge', 'gtsummary')"

How to generate the stratified table with an overall column?


Solution

  • Using add_overall you can add an overall column to tbl_summary. To add an overall column to a stratified table requires a different approach, e.g. create a separate unstratified summary table, then use tbl_merge to merge the stratified and unstratified tables.

    Using the default example from gtsummary:

    library(gtsummary)
    
    dat <- trial |>
      select(age, grade, stage, trt) |>
      mutate(grade = paste("Grade", grade))
    
    list(
      dat |>
        tbl_strata(
          strata = grade,
          .tbl_fun =
            ~ .x |>
            tbl_summary(by = trt, missing = "no") |>
            add_overall(last = TRUE),
          .header = "**{strata}**, N = {n}"
        ),
      dat |>
        select(-grade) |> 
        tbl_summary(
          by = trt, missing = "no"
        ) |> 
        add_overall(last = TRUE) |> 
        modify_spanning_header(
          all_stat_cols() ~ '**All Grades**, N = {N}'
        )
    ) |> 
      tbl_merge(tab_spanner = FALSE)
    

    enter image description here