With numeric values in a vector, we can format the values with SI (International System of Units) prefixes. These prefixes like k (kilo), M (mega), G (giga), and m (milli) scale numbers in either powers of 1000 (engineering mode) or powers of 10 and 100 (decimal mode).
We have control over the formatting with the following options:
decimals: choice of the number of decimal places, option to drop trailing zeros, and a choice of the decimal symbol
scaling: we can choose to scale targeted values by a multiplier value (useful for unit conversions)
SI prefix mode: choose between engineering prefixes (powers of 1000) or decimal prefixes (all SI prefixes)
units: an optional unit designation for the values
pattern: option to use a text pattern for decoration of the formatted values
locale-based formatting: providing a locale ID will result in formatting specific to the chosen locale
Usage
vec_fmt_number_si(
x,
unit = NULL,
decimals = 2,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
scale_by = 1,
prefix_mode = c("engineering", "decimal"),
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign = FALSE,
incl_space = TRUE,
locale = NULL,
output = c("auto", "plain", "html", "latex", "rtf", "word")
)
Arguments
- x
The input vector
vector(numeric|integer)
// requiredThis is the input vector that will undergo transformation to a character vector of the same length. Values within the vector will be formatted.
- unit
Unit to append to formatted values
scalar<character>
// default:NULL
(optional
)A character string specifying the unit to append after the SI prefix (e.g.,
"g"
for grams,"W"
for watts,"Hz"
for hertz,"m"
for meters). IfNULL
, only the prefix will be shown. The unit can also be dynamically specified per row usingfrom_column()
.- decimals
Number of decimal places
scalar<numeric|integer>(val>=0)
// default:2
The exact number of decimal places to display in the mantissa. If both
decimals
andn_sigfig
are provided,n_sigfig
takes precedence.- drop_trailing_zeros
Drop trailing zeros
scalar<logical>
// default:FALSE
Remove trailing zeros after the decimal point (e.g., "1.50" becomes "1.5").
- drop_trailing_dec_mark
Drop trailing decimal mark
scalar<logical>
// default:TRUE
Remove the decimal mark if all decimal places are zero (e.g., "1." becomes "1").
- scale_by
Scale values by a fixed multiplier
scalar<numeric|integer>
// default:1
All numeric values will be multiplied by the
scale_by
value before undergoing formatting. Since thedefault
value is1
, no values will be changed unless a different multiplier value is supplied. This is useful for unit conversions, such as usingunit_conversion()
to convert horsepower to watts before formatting with SI prefixes.- prefix_mode
Type of SI prefixes to use
singl-kw:[engineering|decimal]
// default:"engineering"
The type of SI prefixes to use. Options are
"engineering"
(powers of 1000 only) or"decimal"
(all SI prefixes including powers of 10 and 100). See the SI Prefix Modes section for details.- pattern
Decoration pattern
scalar<character>
// default:"{x}"
A formatting pattern for decorating values. Use
{x}
to represent the formatted value (including prefix and unit).- sep_mark
Thousands separator
scalar<character>
// default:","
The character to use as the thousands separator. Overridden if
locale
is provided.- dec_mark
Decimal mark
scalar<character>
// default:"."
The character to use as the decimal point. Overridden if
locale
is provided.- force_sign
Force positive sign
scalar<logical>
// default:FALSE
Force the display of a plus sign for positive values.
- incl_space
Include a space between the value and the unit symbol
scalar<logical>
// default:TRUE
An option for whether to include a space between the numerical value and the SI prefix + unit (e.g.,
TRUE
for "1.5 kW",FALSE
for "1.5kW"). Per SI convention, there should be a space between the value and the unit symbol.- locale
Locale identifier
scalar<character>
// default:NULL
(optional
)An optional locale identifier that can be used for formatting values according to the locale's 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.- output
Output format
singl-kw:[auto|plain|html|latex|rtf|word]
// default:"auto"
The output style of the resulting character vector. This can either be
"auto"
(the default),"plain"
,"html"
,"latex"
,"rtf"
, or"word"
. In knitr rendering (i.e., Quarto or R Markdown), the"auto"
option will choose the correctoutput
value
Examples
Let's create a numeric vector for the next few examples:
num_vals <- c(1.5e9, 2.7e6, 4200, 0.3, 0.00012, 2.4e-8)
Using vec_fmt_number_si()
with the default options will create a
character vector with values formatted with SI prefixes. Any NA
values
remain as NA
values. The rendering context will be autodetected unless
specified in the output
argument (here, it is of the "plain"
output
type).
vec_fmt_number_si(num_vals)
We can add a unit designation to all values with the unit
option:
vec_fmt_number_si(num_vals, unit = "W")
We can change the number of decimal places with the decimals
option:
vec_fmt_number_si(num_vals, unit = "g", decimals = 0)
If we are formatting for a different locale, we could supply the locale ID and gt will handle any locale-specific formatting options:
vec_fmt_number_si(num_vals, unit = "m", locale = "fr")
The scale_by
option is useful for unit conversions. For instance, to
convert meters to millimeters:
vec_fmt_number_si(c(0.5, 1.2, 3.8), unit = "m", scale_by = 1000)
We can control the space between the number and unit with incl_space
:
vec_fmt_number_si(num_vals, unit = "Hz", incl_space = FALSE, decimals = 1)
As a last example, one can wrap the values in a pattern with the pattern
argument. Note here that NA
values won't have the pattern applied.
vec_fmt_number_si(num_vals, pattern = "[{x}]")
See also
The variant function intended for formatting gt table data:
fmt_number_si()
.
Other vector formatting functions:
vec_fmt_bytes()
,
vec_fmt_currency()
,
vec_fmt_date()
,
vec_fmt_datetime()
,
vec_fmt_duration()
,
vec_fmt_engineering()
,
vec_fmt_fraction()
,
vec_fmt_index()
,
vec_fmt_integer()
,
vec_fmt_markdown()
,
vec_fmt_number()
,
vec_fmt_partsper()
,
vec_fmt_percent()
,
vec_fmt_roman()
,
vec_fmt_scientific()
,
vec_fmt_spelled_num()
,
vec_fmt_time()