Align all numeric values in a column along the decimal mark
Source:R/cols_align_decimal.R
cols_align_decimal.Rd
For numeric columns that contain values with decimal portions, it is
sometimes useful to have them lined up along the decimal mark for easier
readability. We can do this with cols_align_decimal()
and provide any
number of columns (the function will skip over columns that don't require
this type of alignment).
Usage
cols_align_decimal(data, columns = everything(), dec_mark = ".", locale = NULL)
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>
// default:everything()
The columns for which decimal alignment 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()
). By default this is set toeverything()
which means that the decimal alignment affects all columns.- dec_mark
Decimal mark
scalar<character>
// default:"."
The character used as a decimal mark in the numeric values to be aligned. If a locale value was used when formatting the numeric values then
locale
is better to use and it will override any value here indec_mark
.- locale
Locale identifier
scalar<character>
// default:NULL
(optional
)An optional locale identifier that can be used to obtain the type of decimal mark used in the numeric values to be aligned (according to the locale's formatting rules). Examples include
"en"
for English (United States) and"fr"
for French (France). We can callinfo_locales()
for a useful reference for all of the locales that are supported. A locale ID can be also set in the initialgt()
function call (where it would be used automatically by any function with alocale
argument) but alocale
value provided here will override that global locale.
Examples
Let's put together a two-column table to create a gt table. The first
column char
just contains letters whereas the second column, num
, has a
collection of numbers and NA
values. We could format the numbers with
fmt_number()
and elect to drop the trailing zeros past the decimal mark
with drop_trailing_zeros = TRUE
. This can leave formatted numbers that are
hard to scan through because the decimal mark isn't fixed horizontally. We
could remedy this and align the numbers by the decimal mark with
cols_align_decimal()
.
dplyr::tibble(
char = LETTERS[1:9],
num = c(1.2, -33.52, 9023.2, -283.527, NA, 0.401, -123.1, NA, 41)
) |>
gt() |>
fmt_number(
columns = num,
decimals = 3,
drop_trailing_zeros = TRUE
) |>
cols_align_decimal()
See also
Other column modification functions:
cols_add()
,
cols_align()
,
cols_hide()
,
cols_label()
,
cols_label_with()
,
cols_merge()
,
cols_merge_n_pct()
,
cols_merge_range()
,
cols_merge_uncert()
,
cols_move()
,
cols_move_to_end()
,
cols_move_to_start()
,
cols_nanoplot()
,
cols_unhide()
,
cols_units()
,
cols_width()