rgtsummary

Gtsummary split add_n between groups


Example data

library(gtsummary)
trial2 <- trial %>% select(trt, age, grade)


trial2 %>%
  tbl_summary(by = trt) %>%
  add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
  add_overall() %>%
  add_n() %>%
  modify_header(label ~ "**Variable**") %>%
  modify_spanning_header(c("stat_1", "stat_2") ~ "**Treatment Received**") %>%
  modify_footnote(
    all_stat_cols() ~ "Median (IQR) or Frequency (%)"
  ) %>%
  modify_caption("**Table 1. Patient Characteristics**") %>%
  bold_labels()

Output provides this enter image description here

How can I split the N listed for each variable by group, which currently refers to the n available for the overall cohort, so that the columns would be like Variable name, N's available for group 1, Group 1, N's available for group 2, p value


Solution

  • EDIT TO ACCOUNT FOR A CHANGE IN THE INTERNAL GTSUMMARY TABLE STRUCTURE IN v2.0.0.

    You can add custom statistics using the add_stat() function. Example below!

    library(gtsummary)
    
    add_by_n <- function(data, variable, by, ...) {
      data |> 
        select(all_of(c(variable, by))) |> 
        dplyr::arrange(pick(all_of(c(by, variable)))) |> 
        dplyr::group_by(.data[[by]]) |> 
        dplyr::summarise_all(~sum(!is.na(.))) %>%
        rlang::set_names(c("by", "variable")) %>%
        mutate(
          by_col = paste0("add_n_stat_", dplyr::row_number()),
          variable = style_number(variable)
        ) %>%
        select(-by) %>%
        tidyr::pivot_wider(names_from = by_col, 
                           values_from = variable)
    }
      
    tbl <- 
      trial %>%
      select(trt, age, grade) %>%
      tbl_summary(by = trt) %>%
      add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
      add_overall() %>%
      add_n() %>%
      add_stat(
        fns = everything() ~ add_by_n
      ) %>%
      modify_header(starts_with("add_n_stat") ~ "**N**") %>%
      modify_table_body(
        ~ .x %>%
          dplyr::relocate(add_n_stat_1, .before = stat_1) %>%
          dplyr::relocate(add_n_stat_2, .before = stat_2)
      ) %>%
      modify_spanning_header(
        c(all_stat_cols(F), starts_with("add_n_stat")) ~ "**Treatment Received**"
      )
    

    enter image description here Created on 2021-08-06 by the reprex package (v2.0.0)