numeric_friendly {friendlynumber} | R Documentation |
Translate a numeric vector to a cardinal character vector
Description
Convert a numeric vector to a cardinal numeral (e.g. one tenth, one, two).
numeric_friendly_safe()
checks that all arguments are of the correct type
and raises an informative error otherwise. numeric_friendly()
does not
perform input validation to maximize its speed.
Usage
numeric_friendly(
numbers,
zero = "zero",
na = "missing",
nan = "not a number",
inf = "infinity",
negative = "negative ",
decimal = " and ",
and = FALSE,
hyphenate = TRUE,
and_fractional = and,
hyphenate_fractional = hyphenate,
english_fractions = NULL
)
numeric_friendly_safe(
numbers,
zero = "zero",
na = "missing",
nan = "not a number",
inf = "infinity",
negative = "negative ",
decimal = " and ",
and = FALSE,
hyphenate = TRUE,
and_fractional = and,
hyphenate_fractional = hyphenate,
english_fractions = NULL
)
Arguments
numbers |
A numeric vector to translate. |
zero |
What to call values of |
na |
What to call values of |
nan |
What to call values of |
inf |
What to call values of |
negative |
A prefix added to the translation of negative elements of |
decimal |
A word inserted between the whole and fractional part of translated
|
and |
Whether to insert an |
hyphenate |
Whether to hyphenate numbers 21 through 99 (e.g. |
and_fractional |
Whether to insert an
|
hyphenate_fractional |
Whether to hyphenate numbers 21 through 99 in the fractional part of translated
|
english_fractions |
A named character vector used as a dictionary for the translation of the
fractional part of For example By default Provide an empty character to |
Value
A non-NA character vector of the same length as numbers
.
Examples
numeric_friendly(c(1/3, 0, 0.999, NA, NaN, Inf, -Inf))
# Specify the translations of "special" numbers
numeric_friendly(c(1, 0, Inf), zero = "none", inf = "all")
# Modify the output formatting
frac <- 8765.4321
numeric_friendly(frac)
numeric_friendly(frac, decimal = " dot ")
numeric_friendly(frac, hyphenate = TRUE, hyphenate_fractional = FALSE)
numeric_friendly(frac, and = TRUE, and_fractional = TRUE, decimal = " . ")
# The `friendlynumber.numeric.digits` option specifies the number of
# numeric digits mentioned by `numeric_friendly()`
opts <- options()
options(friendlynumber.numeric.digits = 5)
numeric_friendly(0.0987654321)
options(friendlynumber.numeric.digits = 10)
numeric_friendly(0.0987654321)
options(opts)
# Set `english_fractions` to specify the translation of certain
# fractions. The names (keys) of `english_fractions` should match
# the decimal part of a fraction (e.g. `"5"` matches `0.5`).
numeric_friendly(
c(1/2, 6/5, 12),
english_fractions = c(`5` = "1/2", `2` = "1/5")
)
# Input validation
try(numeric_friendly_safe("A"))