MyNixOS website logo
Description

Number Names.

Converts numeric vectors to character vectors of English number names. Provides conversion to cardinals, ordinals, numerators, and denominators. Supports negative and non-integer numbers.

nombre

License:MIT R buildstatus 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, fractions, and ratios.

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, fractions, and ratios:

nom_card(-2)
#> [1] "negative two"
nom_card(9.75)
#> [1] "nine and three quarters"
nom_ratio(0.25)
#> [1] "one in four"
nom_ratio(3)
#> [1] "three in one"

Math with nombres

nombre implements an S3 class that seamlessly decides when to treat nombres like characters and when to treat them like numerics.

x <- nom_card(25)
x
#> [1] "twenty-five"
x + 2
#> [1] "twenty-seven"
sqrt(x)
#> [1] "five"
x < 30
#> [1] TRUE
x == "twenty-five"
#> [1] TRUE

Reverse it

uncardinal() attempts to convert character vectors of cardinal number names to numerics.

uncardinal(c("twenty-five", "negative three", "infinity"))
#> [1]  25  -3 Inf

Advantages 🚀

nombre is implemented using vectorized base R and runs faster than alternatives like english:

bench::mark(as.character(nom_card(1:1000)), as.character(english::english(1:1000)))
#> # A tibble: 2 × 6
#>   expression                                  min   median `itr/sec` mem_alloc
#>   <bch:expr>                             <bch:tm> <bch:tm>     <dbl> <bch:byt>
#> 1 as.character(nom_card(1:1000))           9.26ms   10.7ms     92.5     1.06MB
#> 2 as.character(english::english(1:1000)) 132.24ms  136.9ms      7.27  414.38KB
#> # … with 1 more variable: gc/sec <dbl>

Hex sticker image adapted from artwork by @allison_horst.

Hex sticker fonts are Source Sans 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.

Metadata

Version

0.4.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows