Learn R Programming

gt (version 0.8.0)

cols_align_decimal: Align all numeric values in a column along the decimal mark

Description

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)

Value

An object of class gt_tbl.

Arguments

data

A table object that is created using the gt() function.

columns

The columns for which the alignment should be applied. By default this is set to everything() which means that the chosen alignment affects all columns.

dec_mark

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 in dec_mark.

locale

An optional locale ID that can be used to obtain the type of decimal mark used in the numeric values to be aligned. Examples include "en" for English (United States) and "fr" for French (France). The use of a valid locale ID will override any value provided in dec_mark. We can use the info_locales() function as a useful reference for all of the locales that are supported. Any locale value provided here will override any global locale setting performed in gt()'s own locale argument.

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()

This image of a table was generated from the first code example in the `cols_align_decimal()` help file.

Function ID

4-2

See Also

Other column modification functions: cols_align(), cols_hide(), cols_label(), cols_merge_n_pct(), cols_merge_range(), cols_merge_uncert(), cols_merge(), cols_move_to_end(), cols_move_to_start(), cols_move(), cols_unhide(), cols_width()