Skip to contents

Convert an integer vector, or numeric vector which is coercible to an integer without loss of precision, to a quantifier (e.g. no, the, every, all five).

quantifier_friendly_safe() checks that all arguments are of the correct type and raises an informative error otherwise. quantifier_friendly() does not perform input validation to maximize its speed.

Usage

quantifier_friendly(
  numbers,
  one = "the",
  two = "both",
  zero = "no",
  na = "a missing",
  nan = "an undefined",
  inf = "every",
  negative = "negative ",
  and = FALSE,
  hyphenate = TRUE,
  bigmark = TRUE,
  max_friendly = 100
)

quantifier_friendly_safe(
  numbers,
  one = "the",
  two = "both",
  zero = "no",
  na = "a missing",
  nan = "an undefined",
  inf = "every",
  negative = "negative ",
  and = FALSE,
  hyphenate = TRUE,
  bigmark = TRUE,
  max_friendly = 100
)

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

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.

bigmark

[TRUE / FALSE]

Whether the thousands places of formatted numbers should be separated with a comma (e.g. "10,000,000" vs. "10000000"). bigmark is TRUE by default.

max_friendly

[numeric]

The maximum number to convert to a numeral. Elements of numbers above max_friendly are converted to formatted numbers (e.g. "all 1,000" instead of "all one thousand"). max_friendly is 100 by default.

Use the bigmark argument to determine whether these formatted numbers are comma separated (e.g. "all 1,000" vs. "all 1000").

Value

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

Examples

quantifier_friendly(c(0, 1, 2, 3, NA, NaN, Inf))
#> [1] "no"           "the"          "both"         "all three"    "a missing"   
#> [6] "an undefined" "every"       

# The `negative` prefix appears after the `"all"` prefix
quantifier_friendly(-4)
#> [1] "all negative four"

# `-1` and `-2` are not translated using `one` and `two`
quantifier_friendly(c(1, 2, -1, -2), one = "the", two = "both")
#> [1] "the"              "both"             "all negative one" "all negative two"

# Suppress the translation of large numbers
quantifier_friendly(c(99, 1234), max_friendly = -Inf)
#> [1] "all 99"    "all 1,234"
quantifier_friendly(c(99, 1234), max_friendly = 100)
#> [1] "all ninety-nine" "all 1,234"      
quantifier_friendly(c(99, 1234), max_friendly = 1500)
#> [1] "all ninety-nine"                         
#> [2] "all one thousand two hundred thirty-four"

# Specify the translations of "special" numbers
quantifier_friendly(c(1, Inf), one = "a", inf = "all")
#> [1] "a"   "all"

# Arguments `one`, `two`, `inf`, etc. take precedence over `max_friendly`
quantifier_friendly(1:3, one = "one", two = "two", max_friendly = -1)
#> [1] "one"   "two"   "all 3"

# Modify the output formatting
quantifier_friendly(1021, max_friendly = Inf)
#> [1] "all one thousand twenty-one"
quantifier_friendly(1021, and = TRUE, max_friendly = Inf)
#> [1] "all one thousand and twenty-one"
quantifier_friendly(1021, hyphenate = FALSE, max_friendly = Inf)
#> [1] "all one thousand twenty one"
quantifier_friendly(1021, bigmark = FALSE, max_friendly = 10)
#> [1] "all 1021"
quantifier_friendly(1021, bigmark = TRUE, max_friendly = 10)
#> [1] "all 1,021"

# Input validation
try(quantifier_friendly_safe(1234, max_friendly = NA))
#> Error : `max_friendly` must be a number, not `NA`.