Summarize categorical data by 2 categories in R using gtsummary

72 views Asked by At

My input data has 3 columns - stent_type, success, stent_30. stent_type is either a category 0 or 1, success is either a 0 or 1, and stent_30 is either a 0 or 1 (basically was the stent placed before or after 30 days). I would like to create a 2x3 table with the percent/number of successfully placed stents for category 0 or 1 stents, along with a p-value comparing the two columns for stent_30. Something like this:

Stent < 30 Stent > 30 p-value
stent 0 75%(10) 70% (9) 0.1
stent 1 50% (6) 40% (2) 0.05

I'm basically having trouble because I'm not sure how to summarize a category based on 2 different categories (if that makes sense?)

Sample input data here:

structure(list(success = c(1, 1, 1, 1, 1), stent_type = c(1, 
1, 1, 0, 1), stent_30 = c(TRUE, TRUE, FALSE, FALSE, TRUE)), row.names = c(NA, 
5L), class = "data.frame")
1

There are 1 answers

0
Jay Bee On
library(tidyverse)
library(gtsummary)

df <- structure(list(success = c(1, 1, 1, 1, 1), stent_type = c(1, 
                                                                1, 1, 0, 1), stent_30 = c(TRUE, TRUE, FALSE, FALSE, TRUE)), row.names = c(NA, 
                                                                                                                                          5L), class = "data.frame")
label(df$stent_type) <- "Stent Type"
label(df$stent_30) <- "Time"
label(df$success) <- "Success"

df$stent_type <- factor(df$stent_type, levels = c(0, 1), labels = c("Type A", "Type B"))
df$stent_30 <- factor(df$stent_30, levels = c(TRUE, FALSE), labels = c("< 30 Days", "> 30 Days"))
df$success <- factor(df$success, levels = c(0, 1), labels = c("Failure", "Success"))

df %>%
  tbl_strata(
    strata = stent_type,
    ~.x %>%
      tbl_summary(
        by = stent_30,
        value = list(success ~ "Success"),
        statistic = list(all_categorical() ~ "{n} ({p}%)"),
        missing = "no"
      ) %>%
      add_p(test = all_categorical() ~ "fisher.test")
  )

With credit to the answer from BigMistake here.