Translate integer-ish numbers to a character vector of counts (once, twice, three times)
Source:R/ntimes-friendly.R
ntimes_friendly.RdConvert an integer vector, or numeric vector which is coercible to an integer without loss of precision, to a count (e.g. no times, once, twice, four times).
ntimes_friendly_safe() checks that all arguments are of the correct type
and raises an informative error otherwise. ntimes_friendly() does not
perform input validation to maximize its speed.
Usage
ntimes_friendly(
numbers,
one = "once",
two = "twice",
three = "three times",
zero = "no times",
na = "an unknown number of times",
nan = "an undefined number of times",
inf = "infinite times",
negative = "negative ",
and = FALSE,
hyphenate = TRUE
)
ntimes_friendly_safe(
numbers,
one = "once",
two = "twice",
three = "three times",
zero = "no times",
na = "an unknown number of times",
nan = "an undefined number of times",
inf = "infinite times",
negative = "negative ",
and = FALSE,
hyphenate = TRUE
)Arguments
- numbers
[integer / numeric]An integer or integer-ish numeric vector to translate.
- one
[character(1)]What to call values of
1innumbers(e.g.one = "the").- two
[character(1)]What to call values of
2innumbers(e.g.two = "both").- three
[character(1)]What to call values of
3innumbers(e.g.three = "thrice").- zero
[character(1)]What to call values of
0innumbers(e.g.zero = "zero").- na
[character(1)]What to call values of
NAinnumbers(e.g.na = "missing").- nan
[character(1)]What to call values of
NaNinnumbers(e.g.nan = "undefined").- inf
[character(1)]What to call values of
Infinnumbers(e.g.inf = "infinity").- negative
[character(1)]A prefix added to the translation of negative elements of
numbers.negativeis the string"negative "by default.- and
[TRUE / FALSE]Whether to insert an
" and "before the tens place of translatednumbers.andisFALSEby default.- hyphenate
[TRUE / FALSE]Whether to hyphenate numbers 21 through 99 (e.g.
"twenty-one"vs."twenty one").hyphenateisTRUEby default.
Examples
ntimes_friendly(c(0, 1, 2, 3, 22, 1001, NA, NaN, Inf, -Inf))
#> [1] "no times" "once"
#> [3] "twice" "three times"
#> [5] "twenty-two times" "one thousand one times"
#> [7] "an unknown number of times" "an undefined number of times"
#> [9] "infinite times" "negative infinite times"
# Specify the translations of "special" numbers
ntimes_friendly(c(3, NA), three = "thrice", na = "some times")
#> [1] "thrice" "some times"
# Modify the output formatting
ntimes_friendly(5678)
#> [1] "five thousand six hundred seventy-eight times"
ntimes_friendly(5678, and = TRUE)
#> [1] "five thousand six hundred and seventy-eight times"
ntimes_friendly(5678, hyphenate = FALSE)
#> [1] "five thousand six hundred seventy eight times"
# Input validation
try(ntimes_friendly_safe(1234, and = " - "))
#> Error : `and` must be `TRUE` or `FALSE`, not the string " - ".