Merge gtsummary tables, e.g. tbl_regression
, tbl_uvregression
, tbl_stack
,
tbl_summary
, tbl_svysummary
, etc.
This function merges like tables.
Generally, this means each of the tables being merged
should have the same structure.
When merging tables with different structures, rows may appear
out of order.
The ordering of rows can be updated with modify_table_body(~dplyr::arrange(.x, ...))
.
Arguments
- tbls
(
list
)
List of gtsummary objects to merge- tab_spanner
(
character
)
Character vector specifying the spanning headers. Must be the same length astbls
. The strings are interpreted withgt::md
. Must be same length astbls
argument. Default isNULL
, and places a default spanning header. IfFALSE
, no header will be placed.- merge_vars
(
character
)
Column names that are used as the merge IDs. The default isNULL
, which merges onc(any_of(c("variable", "row_type", "var_label", "label"), cards::all_ard_groups())
. Any column name included here that does not appear in all tables, will be removed.- tbl_ids
(
character
)
Optional character vector of IDs that will be assigned to the input tables. The ID is assigned by assigning a name to thetbls
list, which is returned inx$tbls
.
Examples
# Example 1 ----------------------------------
# Side-by-side Regression Models
library(survival)
t1 <-
glm(response ~ trt + grade + age, trial, family = binomial) %>%
tbl_regression(exponentiate = TRUE)
t2 <-
coxph(Surv(ttdeath, death) ~ trt + grade + age, trial) %>%
tbl_regression(exponentiate = TRUE)
tbl_merge(
tbls = list(t1, t2),
tab_spanner = c("**Tumor Response**", "**Time to Death**")
)
OR
95% CI
p-value
HR
95% CI
p-value
Abbreviations: CI = Confidence Interval, HR = Hazard Ratio, OR = Odds Ratio
# Example 2 ----------------------------------
# Descriptive statistics alongside univariate regression, with no spanning header
t3 <-
trial[c("age", "grade", "response")] %>%
tbl_summary(missing = "no") %>%
add_n() %>%
modify_header(stat_0 ~ "**Summary Statistics**")
t4 <-
tbl_uvregression(
trial[c("ttdeath", "death", "age", "grade", "response")],
method = coxph,
y = Surv(ttdeath, death),
exponentiate = TRUE,
hide_n = TRUE
)
tbl_merge(tbls = list(t3, t4)) %>%
modify_spanning_header(everything() ~ NA_character_)
Characteristic
N
Summary Statistics1
HR
95% CI
p-value
Abbreviations: CI = Confidence Interval, HR = Hazard Ratio
1 Median (Q1, Q3); n (%)