Easy Summary Tables in R with gtsummary

The gtsummary package lets you produce summary statistics for your numeric and categorical variables, formatted into a neat table. You can include p-values for pairwise comparisons and also split your summary across the categories of one of your covariates.


Subscribe below to get updates on my latest videos, courses, and other useful information.


library(tidyverse)
library(gtsummary)

data(CO2)
head(CO2)

# basic summary table
CO2 %>% select(!c(Plant,conc)) %>% tbl_summary()

# summary split by a categorical variable
CO2 %>% select(!c(Plant,conc)) %>% tbl_summary(by = Type)

# summary split by a categorical variable with p-value
CO2 %>% select(!c(Plant,conc)) %>% 
  tbl_summary(by = Type) %>% add_p()

# include overall, extra heading, custom stats
CO2 %>% select(!c(Plant,conc)) %>%
  tbl_summary(
    by = Type,
    statistic = list(
      all_continuous() ~ "{mean} ({sd})",
      all_categorical() ~ "{n} / {N} ({p}%)"
    ), digits = all_continuous() ~ 2) %>% 
  add_p() %>% add_overall() %>%
  modify_spanning_header(c("stat_1", "stat_2") ~ "**Location**")

# crosstabs
CO2 %>%
  tbl_cross(
    row = Type,
    col = Treatment,
    percent = "cell"
  ) %>%
  add_p()