Skip to contents

Merges two or more ltsummary tables side by side, matching rows on the merge_vars columns. Merging works best with like tables: the same variables summarized the same way, as in regression models of the same terms. The interface is that of gtsummary::tbl_merge().

Every column that is not a merge column is renamed with the table's position (stat_1_1, estimate_2, ...), and each table's styling follows its renamed columns. Rows of the first table come first, in order; rows found only in a later table are appended, which can put them out of order. Reorder them with modify_table_body() if needed.

Usage

tbl_merge(
  tbls,
  tab_spanner = NULL,
  merge_vars = NULL,
  tbl_ids = NULL,
  quiet = FALSE
)

Arguments

tbls

(list)
List of ltsummary tables.

tab_spanner

(character)
Spanning headers placed over each table's columns, one per table. The strings are interpreted as markdown and the header statistics are available in braces, e.g. "**N = {N}**". Default is NULL, which places "**Table 1**", "**Table 2**", ...; FALSE places no spanning headers.

merge_vars

(character)
Columns of table_body the rows are matched on. Columns named here that are not present in every table are dropped. Default is NULL, which matches on variable, row_type, var_label and label (those present in the first table).

tbl_ids

(character)
Optional names for the input tables, one per table, used to name the tbls element of the result.

quiet

(scalar logical)
Whether to suppress the message warning of possible row-ordering problems when the tables are not alike. Default is FALSE.

Value

A table of class c("tbl_merge", "ltsummary") whose tbls element holds the input tables.

Examples

# Example 1 ----------------------------------
# two models of the same terms, side by side
t1 <- glm(response ~ trt + grade + age, trial, family = binomial) |>
  tbl_regression(exponentiate = TRUE)
t2 <- glm(death ~ trt + grade + age, trial, family = binomial) |>
  tbl_regression(exponentiate = TRUE)
tbl_merge(list(t1, t2), tab_spanner = c("**Tumor Response**", "**Death**"))
# Example 2 ---------------------------------- # summary statistics beside univariable models t3 <- trial[c("age", "marker", "grade")] |> tbl_summary(missing = "no") |> add_n() |> modify_header(stat_0 = "**Summary Statistics**") t4 <- tbl_uvregression(trial[c("age", "marker", "grade")], method = lm, y = age, hide_n = TRUE) tbl_merge(list(t3, t4)) |> remove_spanning_header(columns = everything()) #> The number rows in the tables to be merged do not match, which may result in rows appearing out of order. #> ℹ See `tbl_merge()` help file for details. Use `quiet=TRUE` to silence message.