Re: [R] How to create a vector with "one", "two", "three", ...?

From: Frank Duan <fhduan_at_gmail.com>
Date: Sun 17 Apr 2005 - 19:09:43 EST

Dear John,

That's exactly what I want.

Millions of thanks,

Frank

On 4/16/05, John Fox <jfox@mcmaster.ca> 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:
>
> > numbers2words(56734200004350)
> [1] "fifty six trillion seven hundred thirty four billion two hundred
> million four thousand three hundred fifty"
> > numbers2words(c(5673420000, 604))
> [1] "five billion six hundred seventy three million four hundred twenty
> thousand"
> [2] "six hundred four"
>
> > numbers2words(21:30)
> [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
> --------------------------------
>
> > -----Original Message-----
> > From: r-help-bounces@stat.math.ethz.ch
> > [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Frank Duan
> > Sent: Saturday, April 16, 2005 5:02 PM
> > To: f.calboli@imperial.ac.uk
> > Cc: r-help
> > Subject: Re: [R] How to create a vector with "one", "two",
> > "three", ...?
> >
> > 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@imperial.ac.uk> wrote:
> > > On Fri, 2005-04-15 at 14:30 -0400, Frank Duan wrote:
> > > > 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", ...?
> > >
> > > rvect <- c("one", "two", "three")
> > > rvect
> > > [1] "one" "two" "three"
> > >
> > > Is it what you want?
> > >
> > > F
> > >
> > > --
> > > Federico C. F. Calboli
> > > Department of Epidemiology and Public Health Imperial College, St
> > > Mary's Campus Norfolk Place, London W2 1PG
> > >
> > > Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193
> > >
> > > f.calboli [.a.t] imperial.ac.uk
> > > f.calboli [.a.t] gmail.com
> > >
> > >
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
>
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sun Apr 17 19:17:40 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:13 EST