Search code examples
gtsummarygt

Using inline to get group size in gtsummary


in gtsummary I would like to use the inline_text to get the number of observations in a category. For example, I would like to get the number and percentage of patients (N = 98) taking Drug A in the following table. Dataframe trial with the package gtsummary.

library(gtsummary)
tab1 <- trial %>% 
     select(trt, marker, stage) %>% 
     tbl_summary(by = trt) %>% 
     add_overall()

This results in this table:

enter image description here

I know that you get values for the whole group using column = "stat_0". For example:

r inline_text(tab1, variable = stage, level = "T1", column = "stat_0")


Solution

  • You can access the total N with inline_text() using the pattern= argument.

    library(gtsummary)
    packageVersion("gtsummary")
    #> [1] '1.4.0'
    
    tbl <- 
      trial %>%
      select(marker, grade, trt) %>%
      tbl_summary(by = trt)
    
    inline_text(tbl, variable = "marker", column = "stat_1", pattern = "{N_obs}")
    #> [1] "98"
    

    Created on 2021-04-26 by the reprex package (v2.0.0)