Rounds and formats numeric vectors for display. Rounding is half away from
zero (0.5 becomes 1), the big mark is inserted for absolute values of
1,000 or more, and missing values are returned as na. The defaults for
big.mark and decimal.mark can be set globally with
options(ltsummary.big.mark = , ltsummary.decimal.mark = ); when the
decimal mark is a comma the default big mark is a thin space.
Arguments
- x
(
numeric)
Numeric vector.- digits
(
integer)
Number of decimal places. Recycled to the length ofx.- big.mark
(
string)
Character used between every three digits to the left of the decimal mark.- decimal.mark
(
string)
Character used as the decimal mark.- scale
(
scalar numeric)
Values are multiplied byscalebefore rounding, e.g.scale = 100to show a proportion as a percentage.- prefix, suffix
(
string)
Text added before and after each formatted value.- na
(
string)
Replacement for missing values.- ...
Not used. Accepted so that formatting functions can be called interchangeably.
See also
Other style helpers:
label_style,
style_percent(),
style_pvalue(),
style_ratio(),
style_sigfig()
Examples
c(0.111, 12.3, 1234.5678, -0.5, NA) |> style_number(digits = 1)
#> [1] "0.1" "12.3" "1,234.6" "-0.5" NA
c(0.111, 12.3) |> style_number(digits = c(1, 0))
#> [1] "0.1" "12"
# half away from zero, unlike round()
style_number(c(0.5, 1.5, 2.5))
#> [1] "1" "2" "3"
round(c(0.5, 1.5, 2.5))
#> [1] 0 2 2
style_number(0.123, digits = 1, scale = 100, suffix = "%")
#> [1] "12.3%"
style_number(1234.5, decimal.mark = ",")
#> [1] "1 235"