quantifier_friendly {friendlynumber}R Documentation

Translate integer-ish numbers to a character vector of quantifiers (the, both, all three)

Description

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

# The `negative` prefix appears after the `"all"` prefix
quantifier_friendly(-4)

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

# Suppress the translation of large numbers
quantifier_friendly(c(99, 1234), max_friendly = -Inf)
quantifier_friendly(c(99, 1234), max_friendly = 100)
quantifier_friendly(c(99, 1234), max_friendly = 1500)

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

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

# Modify the output formatting
quantifier_friendly(1021, max_friendly = Inf)
quantifier_friendly(1021, and = TRUE, max_friendly = Inf)
quantifier_friendly(1021, hyphenate = FALSE, max_friendly = Inf)
quantifier_friendly(1021, bigmark = FALSE, max_friendly = 10)
quantifier_friendly(1021, bigmark = TRUE, max_friendly = 10)

# Input validation
try(quantifier_friendly_safe(1234, max_friendly = NA))

[Package friendlynumber version 1.0.0 Index]