Skip to contents

Convert 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 1 in numbers (e.g. one = "the").

two

[character(1)]

What to call values of 2 in numbers (e.g. two = "both").

three

[character(1)]

What to call values of 3 in numbers (e.g. three = "thrice").

zero

[character(1)]

What to call values of 0 in numbers (e.g. zero = "zero").

na

[character(1)]

What to call values of NA in numbers (e.g. na = "missing").

nan

[character(1)]

What to call values of NaN in numbers (e.g. nan = "undefined").

inf

[character(1)]

What to call values of Inf in numbers (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 translated numbers. and is FALSE by default.

hyphenate

[TRUE / FALSE]

Whether to hyphenate numbers 21 through 99 (e.g. "twenty-one" vs. "twenty one"). hyphenate is TRUE by default.

Value

A non-NA character vector of the same length as numbers.

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 " - ".