Translate integer-ish numbers to an ordinal character vector
Source:R/ordinal-friendly.R
ordinal_friendly.Rd
Convert an integer vector, or numeric vector which is coercible to an integer without loss of precision, to an ordinal numeral (e.g. first, second, third).
ordinal_friendly_safe()
checks that all arguments are of the correct type
and raises an informative error otherwise. ordinal_friendly()
does not
perform input validation to maximize its speed.
Usage
ordinal_friendly(
numbers,
zero = "zeroth",
na = "missingth",
nan = "not a numberth",
inf = "infinitieth",
negative = "negative ",
and = FALSE,
hyphenate = TRUE
)
ordinal_friendly_safe(
numbers,
zero = "zeroth",
na = "missingth",
nan = "not a numberth",
inf = "infinitieth",
negative = "negative ",
and = FALSE,
hyphenate = TRUE
)
Arguments
- numbers
[integer / numeric]
An integer or integer-ish numeric vector to translate.
- zero
[character(1)]
What to call values of
0
innumbers
(e.g.zero = "zero"
).- na
[character(1)]
What to call values of
NA
innumbers
(e.g.na = "missing"
).- nan
[character(1)]
What to call values of
NaN
innumbers
(e.g.nan = "undefined"
).- inf
[character(1)]
What to call values of
Inf
innumbers
(e.g.inf = "infinity"
).- negative
[character(1)]
A prefix added to the translation of negative elements of
numbers
.negative
is the string"negative "
by default.- and
[TRUE / FALSE]
Whether to insert an
" and "
before the tens place of translatednumbers
.and
isFALSE
by default.- hyphenate
[TRUE / FALSE]
Whether to hyphenate numbers 21 through 99 (e.g.
"twenty-one"
vs."twenty one"
).hyphenate
isTRUE
by default.
Examples
ordinal_friendly(c(0, 1, 2, 3, NA, NaN, Inf, -Inf))
#> [1] "zeroth" "first" "second"
#> [4] "third" "missingth" "not a numberth"
#> [7] "infinitieth" "negative infinitieth"
ordinal_friendly(10^10)
#> [1] "ten billionth"
# Specify the translations of "special" numbers
ordinal_friendly(0, zero = "noneth")
#> [1] "noneth"
# Modify the output formatting
ordinal_friendly(1234)
#> [1] "one thousand two hundred thirty-fourth"
ordinal_friendly(1234, and = TRUE)
#> [1] "one thousand two hundred and thirty-fourth"
ordinal_friendly(1234, hyphenate = FALSE)
#> [1] "one thousand two hundred thirty fourth"
# Input validation
try(ordinal_friendly_safe(0.5))
#> Error : `numbers` must be coercible to an integer without loss of precision.