Merge two or more columns of a table into one. Run show_header_names()
to see the column names.
Arguments
- x
(
ltsummary)
An ltsummary object.- pattern
(
string)
A pattern with column names ofx$table_bodyin curly braces, e.g."{conf.low}, {conf.high}"to build a confidence interval. The merged text is shown in the first column named in the pattern; the other columns are hidden.- rows
(predicate
expression)
Expression selecting rows ofx$table_body, e.g.row_type == "label". Used for footnotes, formatting functions, missing symbols, indentation and text formatting. Default isNULL, which selects every row (or, for a footnote, places it in the column header). See the section below.- columns
(selector)
Columns ofx$table_body.
Details
Calling this function only records the instruction to merge. The merging happens when the table is printed or converted with
as_lt()oras.data.frame.ltsummary(), after the formatting functions have been applied to each column, somodify_fmt_fun()still works on the individual columns.remove_column_merge()undoes merges recorded withmodify_column_merge(). It also undoes merges made by the package, for example the"{label}, {stat_label}"merge ofadd_stat_label().
rows argument
The rows argument is a predicate expression that selects the rows the
styling applies to. It is evaluated in x$table_body, so its column names
are available as variables, and it must give a logical vector. To style
the rows of the age variable pass rows = variable == "age"; to style the
variable label rows pass rows = row_type == "label". A logical vector,
a vector of row numbers or a function of table_body are also accepted.
Objects from the calling environment can be used in the expression, e.g.
rows = variable == vars[1], as long as they do not share a name with a
column of x$table_body. The expression is stored, not its result, and
resolved when the table is printed, so it survives later changes to the
table such as remove_row_type().
See also
Other advanced modifiers:
modify_bold_italic,
modify_column_alignment(),
modify_column_hide(),
modify_fmt_fun(),
modify_indent(),
modify_missing_symbol(),
modify_post_fmt_fun(),
tbl_butcher()
Examples
# Example 1 ----------------------------------
# show the test statistic next to the p-value
trial |>
tbl_summary(by = trt, missing = "no", include = c(age, marker)) |>
add_p(all_continuous() ~ "t.test", pvalue_fun = label_style_pvalue(prepend_p = TRUE)) |>
modify_fmt_fun(statistic ~ label_style_sigfig()) |>
modify_column_merge(pattern = "t = {statistic}; {p.value}") |>
modify_header(statistic = "**t-test**")
# Example 2 ----------------------------------
# merge only in some rows
trial |>
tbl_summary(by = trt, include = c(age, grade), missing = "no") |>
add_p() |>
modify_column_merge(pattern = "{stat_1} / {stat_2}", rows = row_type == "level") |>
modify_header(stat_1 = "**Drug A / Drug B**")