Translate integer-ish numbers to a character vector of nths (1st, 2nd, 3rd)
Source:R/nth-friendly.R
nth_friendly.Rd
Convert an integer vector, or numeric vector which is coercible to an integer without loss of precision, to an "nth" (e.g. 1st, 2nd, 3rd, 22nd, 1,000th).
nth_friendly_safe()
checks that all arguments are of the correct type
and raises an informative error otherwise. nth_friendly()
does not
perform input validation to maximize its speed.
Usage
nth_friendly(
numbers,
zero = "0th",
na = "missingth",
nan = "not a numberth",
inf = "infinitieth",
negative = "negative ",
bigmark = TRUE
)
nth_friendly_safe(
numbers,
zero = "zeroth",
na = "missingth",
nan = "not a numberth",
inf = "infinitieth",
negative = "negative ",
bigmark = 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.- bigmark
[TRUE / FALSE]
Whether the thousands places of formatted numbers should be separated with a comma (e.g.
"10,000,000"
vs."10000000"
).bigmark
isTRUE
by default.
Examples
nth_friendly(c(0, 1, 2, 3, 22, 1001, NA, NaN, Inf, -Inf))
#> [1] "0th" "1st" "2nd"
#> [4] "3rd" "22nd" "1,001st"
#> [7] "missingth" "not a numberth" "infinitieth"
#> [10] "negative infinitieth"
# Specify the translations of "special" numbers
nth_friendly(c(1, 0, NA), zero = "noneth", na = "?")
#> [1] "1st" "noneth" "?"
# Use `bigmark` to add or remove commas
nth_friendly(1234, bigmark = TRUE)
#> [1] "1,234th"
nth_friendly(1234, bigmark = FALSE)
#> [1] "1234th"
# Input validation
try(nth_friendly_safe(1234, bigmark = ","))
#> Error : `bigmark` must be `TRUE` or `FALSE`, not the string ",".