Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

r - Convert integer to words

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...