Search code examples
rgtsummary

gtsummary - displaying N per group within strata


REP

structure(list(sbp = c(127.5, 116, 131, 124.5, 127.5, 130, 145.5, 
125.5), dbp = c(91, 77.5, 85, 88, 84, 87.5, 88, 86.5), group = c("FUTSAL", 
"FUTSAL", "FUTSAL", "CONTROL", "FUTSAL", "CONTROL", "FUTSAL", 
"CONTROL"), time = c("0month", "3month", "4month", "0month", 
"0month", "0month", "3month", "4month")), row.names = c(NA, -8L
), class = c("tbl_df", "tbl", "data.frame"))

Code gtsummary:

test <- BABA %>%
  select( sbp, dbp, group, time)%>%
  apply_labels(               sbp = "SBP (mmHg)",
               dbp = "DBP (mmHg)" 
              )%>%
  tbl_strata(strata = time, 
             .tbl_fun =
               ~ .x %>%
               tbl_summary(by = group,  
                           statistic = list(all_continuous() ~ "{mean} ± {sd} "),
                           type = list(where(is.numeric) ~ "continuous2"),
                                                      missing = "no") %>%
               bstfun::add_variable_grouping( "Blood pressure" = c("sbp", "dbp")) %>%
               add_n( ),
             .header = "**{strata}** "  ) 

Hi. This code produces a column with the total of observations per time point, on the left.

However, I want it to state the N per time for control (yellow), and for futsal (grey) - see image below. That would be more informative as it would also inform in which groups the missings occurred.

Is it possible?

[![enter image description here][1]][1]

Thanks.


Solution

  • To get a new column of N for each by/strata level, you can move the by variable to strata. Example below.

    library(gtsummary)
    
    BABA <- 
      structure(list(sbp = c(127.5, 116, 131, 124.5, 127.5, 130, 145.5, 125.5), 
                     dbp = c(91, 77.5, 85, 88, 84, 87.5, 88, 86.5), 
                     group = c("FUTSAL", "FUTSAL", "FUTSAL", "CONTROL", "FUTSAL", "CONTROL", "FUTSAL", "CONTROL"), 
                     time = c("0month", "3month", "4month", "0month", "0month", "0month", "3month", "4month")), 
                row.names = c(NA, -8L), 
                class = c("tbl_df", "tbl", "data.frame"))
    
    tbl <-
      BABA %>%
      select( sbp, dbp, group, time) %>%
      tbl_strata(
        strata = c(time, group), 
        .tbl_fun =
          ~ .x %>%
          tbl_summary(
            statistic = list(all_continuous() ~ "{mean} ± {sd} "),
            type = list(where(is.numeric) ~ "continuous2"),
            missing = "no") %>%
          add_n( )
      ) 
    

    enter image description here Created on 2022-09-14 with reprex v2.0.2