Skip to content

How to create a vector with "one", "two", "three", ...?

6 messages · Federico Calboli, Frank Duan, Marc Schwartz +1 more

#
Hi R people,

I met a naive prolem. Could anyone give me a hint how to create such a
vector with entries: "one", "two", "three", ...?

I must have seen this problem somewhere else but I can't find that
source now. Sorry to bother you with such a simple problem.

Many thanks,

Frank
#
On Fri, 2005-04-15 at 14:30 -0400, Frank Duan wrote:
rvect <- c("one", "two", "three")
rvect
[1] "one"   "two"   "three"


Is it what you want?

F
1 day later
#
Sorry, I didn't get the question clear. What I meant is to create a
character vector with length 200:
"one", "two", "three", ..., "two hundred"
On 4/15/05, Federico Calboli <f.calboli at imperial.ac.uk> wrote:
#
I may be wrong, but I am unaware of anyone that has created a number to
text function in R.

If you search Google:

http://www.google.com/search?q=numbers+into+words

There are various program examples, from VB to JavaScript to PHP, etc.

It shouldn't be too hard to convert one of them to R. Most have fairly
common constructs in terms of parsing and converting the numbers. Some
of them handle decimals and currency formats as well.

HTH,

Marc Schwartz
On Sat, 2005-04-16 at 18:01 -0400, Frank Duan wrote:
#
Dear Frank,

This was an interesting exercise. Here's a solution:

numbers2words <- function(x){
    helper <- function(x){
        digits <- rev(strsplit(as.character(x), "")[[1]])
        nDigits <- length(digits)
        if (nDigits == 1) as.vector(ones[digits])
        else if (nDigits == 2)
            if (x <= 19) as.vector(teens[digits[1]])
                else trim(paste(tens[digits[2]],
Recall(as.numeric(digits[1]))))
        else if (nDigits == 3) trim(paste(ones[digits[3]], "hundred", 
            Recall(makeNumber(digits[2:1]))))
        else {
            nSuffix <- ((nDigits + 2) %/% 3) - 1
            if (nSuffix > length(suffixes)) stop(paste(x, "is too large!"))
            trim(paste(Recall(makeNumber(digits[
                nDigits:(3*nSuffix + 1)])),
                suffixes[nSuffix],  
                Recall(makeNumber(digits[(3*nSuffix):1]))))
            }
        }
    trim <- function(text){
        gsub("^\ ", "", gsub("\ *$", "", text))
        }      
    makeNumber <- function(...) as.numeric(paste(..., collapse=""))
    opts <- options(scipen=100)
    on.exit(options(opts))
    ones <- c("", "one", "two", "three", "four", "five", "six", "seven", 
        "eight", "nine")
    names(ones) <- 0:9 
    teens <- c("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",

        "sixteen", " seventeen", "eighteen", "nineteen")
    names(teens) <- 0:9
    tens <- c("twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", 
        "ninety")
    names(tens) <- 2:9 
    x <- round(x)
    suffixes <- c("thousand", "million", "billion", "trillion")
    if (length(x) > 1) return(sapply(x, helper))
    helper(x)
    }

For example:
[1] "fifty six trillion seven hundred thirty four billion two hundred
million four thousand three hundred fifty"
[1] "five billion six hundred seventy three million four hundred twenty
thousand"
[2] "six hundred four"
[1] "twenty one"   "twenty two"   "twenty three" "twenty four"  "twenty
five" 
 [6] "twenty six"   "twenty seven" "twenty eight" "twenty nine"  "thirty"
Note that if you want, you could go beyond trillions by adding to suffixes.

I hope that this does what you want,
 John

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
--------------------------------
#
Dear John,

That's exactly what I want. 

Millions of thanks,

Frank
On 4/16/05, John Fox <jfox at mcmaster.ca> wrote: