This function takes input from two or more columns and allows the contents to
be merged them into a single column, using a pattern that specifies the
arrangement. We can specify which columns to merge together in the columns
argument. The string-combining pattern is given in the pattern argument.
The first column in the columns series operates as the target column (i.e.,
will undergo mutation) whereas all following columns will be untouched.
There is the option to hide the non-target columns (i.e., second and
subsequent columns given in columns). The formatting of values in different
columns will be preserved upon merging.
cols_merge(
data,
columns,
hide_columns = columns[-1],
rows = everything(),
pattern = NULL
)An object of class gt_tbl.
A table object that is created using the gt() function.
The columns that will participate in the merging process. The first column name provided will be the target column (i.e., undergo mutation) and the other columns will serve to provide input.
Any column names provided here will have their state
changed to hidden (via internal use of cols_hide() if they aren't
already hidden. This is convenient if the shared purpose of these specified
columns is only to provide string input to the target column. To suppress
any hiding of columns, FALSE can be used here.
Rows that will participate in the merging process. Providing
everything() (the default) results in all rows in columns undergoing
merging. Alternatively, we can supply a vector of row identifiers within
c(), a vector of row indices, or a helper function focused on selections.
The select helper functions are: starts_with(), ends_with(),
contains(), matches(), one_of(), num_range(), and everything().
We can also use a standalone predicate expression to filter down to the
rows we need (e.g., [colname_1] > 100 & [colname_2] < 50).
A formatting pattern that specifies the arrangement of the
column values and any string literals. The pattern uses numbers (within
{ }) that correspond to the indices of columns provided in columns. If
two columns are provided in columns and we would like to combine the cell
data onto the first column, "{1} {2}" could be used. If a pattern isn't
provided then a space-separated pattern that includes all columns will be
generated automatically. Further details are provided in the How the
pattern works section.
There are two types of templating for the pattern string:
{ } for arranging single column values in a row-wise fashion
<< >> to surround spans of text that will be removed if any of the
contained { } yields a missing value
Integer values are placed in { } and those values correspond to the columns
involved in the merge, in the order they are provided in the columns
argument. So the pattern "{1} ({2}-{3})" corresponds to the target column
value listed first in columns and the second and third columns cited
(formatted as a range in parentheses). With hypothetical values, this might
result as the merged string "38.2 (3-8)".
Because some values involved in merging may be missing, it is likely that
something like "38.2 (3-NA)" would be undesirable. For such cases, placing
sections of text in << >> results in the entire span being eliminated if
there were to be an NA value (arising from { } values). We could instead
opt for a pattern like "{1}<< ({2}-{3})>>", which results in "38.2" if
either columns {2} or {3} have an NA value. We can even use a more
complex nesting pattern like "{1}<< ({2}-<<{3}>>)>>" to retain a lower
limit in parentheses (where {3} is NA) but remove the range altogether
if {2} is NA.
One more thing to note here is that if sub_missing() is used on values in
a column, those specific values affected won't be considered truly missing by
cols_merge() (since it's been handled with substitute text). So, the
complex pattern "{1}<< ({2}-<<{3}>>)>>" might result in something like
"38.2 (3-limit)" if sub_missing(..., missing_text = "limit") were used
on the third column supplied in columns.
There are three other column-merging functions that offer specialized
behavior that is optimized for common table tasks: cols_merge_range(),
cols_merge_uncert(), and cols_merge_n_pct(). These functions operate
similarly, where the non-target columns can be optionally hidden from the
output table through the autohide option.
Use a portion of sp500 to create a gt table. Use the cols_merge()
function to merge the open & close columns together, and, the low &
high columns (putting an em dash between both). Relabel the columns with
cols_label().
sp500 |>
dplyr::slice(50:55) |>
dplyr::select(-volume, -adj_close) |>
gt() |>
cols_merge(
columns = c(open, close),
pattern = "{1}—{2}"
) |>
cols_merge(
columns = c(low, high),
pattern = "{1}—{2}"
) |>
cols_label(
open = "open/close",
low = "low/high"
)

Use a portion of gtcars to create a gt table. Use the cols_merge()
function to merge the trq & trq_rpm columns together, and, the mpg_c &
mpg_h columns. Given the presence of NA values, we can use patterns that
drop parts of the output text whenever missing values are encountered.
gtcars |>
dplyr::filter(year == 2017) |>
dplyr::select(mfr, model, starts_with(c("trq", "mpg"))) |>
gt() |>
fmt_integer(columns = trq_rpm) |>
cols_merge(
columns = starts_with("trq"),
pattern = "{1}<< ({2} rpm)>>"
) |>
cols_merge(
columns = starts_with("mpg"),
pattern = "<<{1} city<</{2} hwy>>>>"
) |>
cols_label(
mfr = "Manufacturer",
model = "Car Model",
trq = "Torque",
mpg_c = "MPG"
)

5-11
v0.2.0.5 (March 31, 2020)
Other column modification functions:
cols_align_decimal(),
cols_align(),
cols_hide(),
cols_label_with(),
cols_label(),
cols_merge_n_pct(),
cols_merge_range(),
cols_merge_uncert(),
cols_move_to_end(),
cols_move_to_start(),
cols_move(),
cols_unhide(),
cols_width()