Search code examples
rgtsummary

How to use tbl_summary() in a summarized table


Consider this situation:

df <- structure(list(id = c("patient1", "patient2", "patient3", "patient4", 
"patient5"), x = c("h,a,a", "i", "i", "i,a,e", "h")), class = "data.frame", row.names = c(NA, -5L))

        id     x
1 patient1 h,a,a
2 patient2     i
3 patient3     i
4 patient4 i,a,e
5 patient5     h

If we do:

library(dplyr)
library(tidyr)
library(gt_summary)

df %>% 
  separate_rows(x, sep = ",") %>% 
  select(x) %>% 
  tbl_summary()

We get a summary table with N=9!

enter image description here

But I need something like this done with the gt_summary package to have the same format like the other gt_summary tables:

df %>% 
  separate_rows(x, sep = ",") %>% 
  select(x) %>%
  count(x) %>% 
  mutate(percent = round(n/nrow(df)*100)) %>% 
  mutate(N_percent = paste0(n,"/", nrow(df), " (",percent,"%)"), .keep = "unused") %>% 
  select(x, N = N_percent) %>% 
  gt() %>%
  tab_header(
    title = "Summary of x"
  ) %>%
  cols_label(
    N = "Count"
  )

N should be 5 and not 9 with the corresponding percents:

enter image description here

The final output should be a gt_summary table looking like this, and I wish to do it with gt_summary:

enter image description here


Solution

  • There is a gtsummary extension function, gtreg::tbl_listing(), that creates listings from data frames. You can use it after you prep your data frame for printing (as you did above). Example below!

    library(gtsummary)
    library(gtreg)
    library(tidyverse)
    
    df <- structure(
      list(id = c("patient1", "patient2", "patient3", "patient4", 
                  "patient5"), x = c("h,a,a", "i", "i", "i,a,e", "h")), 
      class = "data.frame", row.names = c(NA, -5L)
    )
    
    
    # prep data for a table
    tbl <- df %>% 
      separate_rows(x, sep = ",") %>% 
      select(x) %>%
      count(x) %>% 
      mutate(percent = round(n/nrow(df)*100)) %>% 
      mutate(N_percent = paste0(n,"/", nrow(df), " (",percent,"%)"), .keep = "unused") %>% 
      select(x, N = N_percent) |> 
      mutate(grouping_var = "x", .before = 1L) |> 
      # create a gtsummary class listing
      tbl_listing(df, group_by = grouping_var) |> 
      # style table with gtusmmary functions to make it look like `tbl_summary()`
      modify_header(
        x ~ "**Characteristic**",
        N ~ "**N = 5**"
      ) |> 
      modify_column_alignment(N, align = "center") |> 
      modify_footnote(N = "n/N (%)")
    

    enter image description here Created on 2023-12-09 with reprex v2.0.2