nombre

License: MIT R build status Dependencies

nombre (French) /nɔ̃bʁ/: number
nombre (Spanish) /ˈnom.bɾe/: name
nombre: package to convert numbers to their names in R

nombre converts numeric vectors to character vectors of English words. You can use it to express numbers as cardinals (one, two, three) or ordinals (first, second, third), as well as numerators and denominators. nombre supports not just whole numbers, but also negatives and fractions.

Installation

You can install the released version of nombre from CRAN with:

install.packages("nombre")

or the development version from GitHub with:

# install.packages("remotes")
remotes::install_github("rossellhayes/nombre")

Usage

nombre converts numerics into words:

nom_card(2)
#> [1] "two"
nom_card(2L)
#> [1] "two"
x <- rep(TRUE, 525600)
nom_card(length(x))
#> [1] "five hundred twenty-five thousand six hundred"

It also works for numeric vectors:

nom_card(8^(1:10))
#>  [1] "eight"                                                                                       
#>  [2] "sixty-four"                                                                                  
#>  [3] "five hundred twelve"                                                                         
#>  [4] "four thousand ninety-six"                                                                    
#>  [5] "thirty-two thousand seven hundred sixty-eight"                                               
#>  [6] "two hundred sixty-two thousand one hundred forty-four"                                       
#>  [7] "two million ninety-seven thousand one hundred fifty-two"                                     
#>  [8] "sixteen million seven hundred seventy-seven thousand two hundred sixteen"                    
#>  [9] "one hundred thirty-four million two hundred seventeen thousand seven hundred twenty-eight"   
#> [10] "one billion seventy-three million seven hundred forty-one thousand eight hundred twenty-four"

nombre can also generate ordinals, adverbials, collectives, numerators and denominators:

nom_ord(1:5)
#> [1] "first"  "second" "third"  "fourth" "fifth"
nom_adv(1:5)
#> [1] "once"        "twice"       "three times" "four times"  "five times"
nom_coll(1:5)
#> [1] "the"       "both"      "all three" "all four"  "all five"
nom_numer(1:5)
#> [1] "one"   "two"   "three" "four"  "five"
nom_denom(1:5)
#> [1] "whole"   "half"    "third"   "quarter" "fifth"
nom_denom(1:5, numerator = 1:5)
#> [1] "whole"    "halves"   "thirds"   "quarters" "fifths"

🤫 (numerators are almost always the same as cardinals)

You can also add ordinal suffixes to numerics or arbitrary number-like strings:

nom_ord(1:5, cardinal = FALSE)
#> [1] "1st" "2nd" "3rd" "4th" "5th"
nom_ord(c("n", "dozen", "umpteen", "eleventy", "one zillion"))
#> [1] "nth"           "dozenth"       "umpteenth"     "eleventieth"  
#> [5] "one-zillionth"

It can also handle less common numerics, like negatives and fractions:

nom_card(-2)
#> [1] "negative two"
nom_card(99.9)
#> [1] "ninety-nine and nine tenths"

Advantages 🚀

nombre is implemented using vectorized base R. This allows it to run faster than options that implement their own object class, like english:

bench::mark(nom_card(1:1000), as.character(english::english(1:1000)))
#> Warning: Some expressions had a GC in every iteration; so filtering is disabled.
#> # A tibble: 2 x 6
#>   expression                                 min  median `itr/sec` mem_alloc
#>   <bch:expr>                             <bch:t> <bch:t>     <dbl> <bch:byt>
#> 1 nom_card(1:1000)                        17.4ms  27.2ms     35.5     1.03MB
#> 2 as.character(english::english(1:1000)) 139.6ms 160.3ms      5.58  389.29KB
#> # ... with 1 more variable: `gc/sec` <dbl>

nombre does not depend on any other packages, making it lightweight to include in your package.


Hex sticker image adapted from artwork by @allison_horst.

Hex sticker fonts are Source Code Pro by Adobe and Permanent Marker by Font Diner.

Please note that nombre is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.