Skip to content

Instantly share code, notes, and snippets.

@ddsjoberg
Created January 20, 2021 15:25
Show Gist options
  • Save ddsjoberg/8bc221be836cc8e25e01d960fc4ca5cf to your computer and use it in GitHub Desktop.
Save ddsjoberg/8bc221be836cc8e25e01d960fc4ca5cf to your computer and use it in GitHub Desktop.
library(gtsummary)
trial %>%
select(marker, trt) %>%
tbl_summary(
by = trt,
missing = "no",
statistic = everything() ~ "{mean} ({sd})",
# use a function to style output, rather than specify number of decimal places
digits = everything() ~ style_sigfig
) %>%
# new function shows the difference between two group
add_difference(
test = everything() ~ "t.test",
# new argument, `test.args`, to pass additional arguments to `test=` method
# new selector, `all_tests()`, chooses variables based on the test name
test.args = all_tests("t.test") ~ list(var.equal = TRUE)
) %>%
# new selector
# new selector, `all_stat_cols()`, chooses the summary stat columns from tbl_summary()
modify_header(all_stat_cols() ~ "**{level}**") %>%
# new function adds a proper table caption that allows for cross-referencing tables
modify_caption("**Packed gtsummary Example**")
trial %>%
select(age, trt) %>%
mutate(age_cat = factor(age < 50, labels = c("50+", "<50"))) %>%
tbl_summary(by = trt, missing = "no") %>%
remove_row_type(variables = age_cat, type = "header")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment