Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer to words

Tags:

r

For the purpose of styling data visualizations, I'd like to be able to display an integer using words (e.g.

"Two thousand and seventeen"

) rather than digits (e.g. 2017).

As an example of what I'm looking for, here's a quick function that works for a small, scalar integer:

int_to_words <- function(x) {

                   index <- as.integer(x) + 1
                   words <- c('zero', 'one', 'two', 'three', 'four',
                              'five', 'six', 'seven', 'eight', 'nine',
                              'ten')
                   words[index]
}


int_to_words(5)
like image 239
bschneidr Avatar asked Oct 09 '17 17:10

bschneidr


1 Answers

Option 1:

Use the as.english function from the 'english' package:

library(english)

as.english(2017)


Option 2:

Use the replace_number function from the 'qdap' package.

library(qdap)

replace_number(2017)


Option 3:

Use the numbers_to_words function from the 'xfun' package.

library(xfun)

numbers_to_words(2017)
like image 174
bschneidr Avatar answered Nov 09 '22 08:11

bschneidr