On those occasions where you need to move columns this way or that way, we
can make use of the cols_move()
function. While it's true that the movement
of columns can be done upstream of gt, it is much easier and less error
prone to use the function provided here. The movement procedure here takes
one or more specified columns (in the columns
argument) and places them to
the right of a different column (the after
argument). The ordering of the
columns
to be moved is preserved, as is the ordering of all other columns
in the table.
Arguments
- data
The gt table data object
obj:<gt_tbl>
// requiredThis is the gt table object that is commonly created through use of the
gt()
function.- columns
Columns to target
<column-targeting expression>
// requiredThe columns for which the moving operations should be applied. Can either be a series of column names provided in
c()
, a vector of column indices, or a select helper function (e.g.starts_with()
,ends_with()
,contains()
,matches()
,num_range()
, andeverything()
. The columns move as a group to a different position. The order of the remaining columns will be preserved.- after
Column used as anchor
<column-targeting expression>
// requiredThe column used to anchor the insertion of the moved columns. All of the moved columns will be placed to the right of this column. While select helper functions such as
starts_with()
andends_with()
can be used for column targeting, it's recommended that a single column name be used. This is to ensure that exactly one column is provided here.
Details
The columns supplied in columns
must all exist in the table and none of
them can be in the after
argument. The after
column must also exist and
only one column should be provided here. If you need to place one or more
columns at the beginning of the column series, the cols_move_to_start()
function should be used. Similarly, if those columns to move should be placed
at the end of the column series then use cols_move_to_end()
.
Examples
Use the countrypops
dataset to create a gt table. We'll choose to
position the population
column after the country_name
column by using the
cols_move()
function.
See also
Other column modification functions:
cols_add()
,
cols_align()
,
cols_align_decimal()
,
cols_hide()
,
cols_label()
,
cols_label_with()
,
cols_merge()
,
cols_merge_n_pct()
,
cols_merge_range()
,
cols_merge_uncert()
,
cols_move_to_end()
,
cols_move_to_start()
,
cols_nanoplot()
,
cols_unhide()
,
cols_units()
,
cols_width()