An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20080302/8ae5fb35/attachment.pl
R function to convert a number to text
7 messages · Jim Lemon, Alberto Monteiro, Gabor Grothendieck +2 more
lin tang wrote:
hi, Dear R users - I wonder is there a written R function to convert a number to a text, say convert 1 to "one" , 100 to "one hundred". I know in xls. has such a function BAHTTEXT, does anybody know is there a similar function in R ? Thanks.
Hi Lin,
I got bored after the millions, but this should handle the smaller
numbers, and you can always extend it.
digits2text<-function(x,mult="") {
units<-c("one","two","three","four","five",
"six","seven","eight","nine")
teens<-c("ten","eleven","twelve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen")
tens<-c("ten","twenty","thirty","forty","fifty",
"sixty","seventy","eighty","ninety")
digits<-rev(as.numeric(strsplit(as.character(x),"")[[1]]))
digilen<-length(digits)
if(digilen == 2 && digits[2] == 1) return(teens[digits[1]+1])
digitext<-units[digits[1]]
if(digilen > 1) digitext<-c(digitext,tens[digits[2]])
if(digilen > 2) digitext<-c(digitext,"hundred",units[digits[3]])
if(digilen > 3) digitext<-
c(digitext,digits2text(floor(x/1000),"thousand"))
if(digilen > 6) digitext<-
c(digitext,digits2text(floor(x/1000000),"million"))
return(paste(c(rev(digitext),mult),sep="",collapse=" "))
}
Jim
Jim Lemon wrote:
I got bored after the millions, but this should handle the smaller
numbers, and you can always extend it.
digits2text<-function(x,mult="") {
units<-c("one","two","three","four","five",
"six","seven","eight","nine")
teens<-c("ten","eleven","twelve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen")
tens<-c("ten","twenty","thirty","forty","fifty",
"sixty","seventy","eighty","ninety")
digits<-rev(as.numeric(strsplit(as.character(x),"")[[1]]))
digilen<-length(digits)
if(digilen == 2 && digits[2] == 1) return(teens[digits[1]+1])
digitext<-units[digits[1]]
if(digilen > 1) digitext<-c(digitext,tens[digits[2]])
if(digilen > 2) digitext<-c(digitext,"hundred",units[digits[3]])
if(digilen > 3) digitext<-
c(digitext,digits2text(floor(x/1000),"thousand"))
if(digilen > 6) digitext<-
c(digitext,digits2text(floor(x/1000000),"million"))
return(paste(c(rev(digitext),mult),sep="",collapse=" "))
}
Be careful, the function does not handle "empty" fields:
digits2text(10^6 + 10)
[1] "one million one thousand hundred thousand hundred ten " Alberto Monteiro (today I am purely destructive)
Try
RSiteSearch("numbers2words")
On Sun, Mar 2, 2008 at 9:05 PM, lin tang <lynnatsd at yahoo.com> wrote:
hi, Dear R users -
I wonder is there a written R function to convert a number to a text, say convert 1 to "one" , 100 to "one hundred". I know in xls. has such a function BAHTTEXT, does anybody know is there a similar function in R ? Thanks.
Lin
---------------------------------
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
check also the article by John Fox in Rnews volume 5/1, May 2005, Programmer's Niche 51-55: http://cran.r-project.org/doc/Rnews/Rnews_2005-1.pdf Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Jim Lemon" <jim at bitwrit.com.au> To: "lin tang" <lynnatsd at yahoo.com> Cc: <r-help at r-project.org> Sent: Monday, March 03, 2008 11:38 AM Subject: Re: [R] R function to convert a number to text
lin tang wrote:
hi, Dear R users - I wonder is there a written R function to convert a number to a text, say convert 1 to "one" , 100 to "one hundred". I know in xls. has such a function BAHTTEXT, does anybody know is there a similar function in R ? Thanks.
Hi Lin,
I got bored after the millions, but this should handle the smaller
numbers, and you can always extend it.
digits2text<-function(x,mult="") {
units<-c("one","two","three","four","five",
"six","seven","eight","nine")
teens<-c("ten","eleven","twelve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen")
tens<-c("ten","twenty","thirty","forty","fifty",
"sixty","seventy","eighty","ninety")
digits<-rev(as.numeric(strsplit(as.character(x),"")[[1]]))
digilen<-length(digits)
if(digilen == 2 && digits[2] == 1) return(teens[digits[1]+1])
digitext<-units[digits[1]]
if(digilen > 1) digitext<-c(digitext,tens[digits[2]])
if(digilen > 2) digitext<-c(digitext,"hundred",units[digits[3]])
if(digilen > 3) digitext<-
c(digitext,digits2text(floor(x/1000),"thousand"))
if(digilen > 6) digitext<-
c(digitext,digits2text(floor(x/1000000),"million"))
return(paste(c(rev(digitext),mult),sep="",collapse=" "))
}
Jim
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Alberto Monteiro wrote:
...
> Be careful, the function does not handle "empty" fields:
Okay, Alberto, you asked for it!
digits2text<-function(x,illion=0) {
units<-c("one","two","three","four","five",
"six","seven","eight","nine")
teens<-c("ten","eleven","twelve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen")
tens<-c("ten","twenty","thirty","forty","fifty",
"sixty","seventy","eighty","ninety")
illions<-c("thousand","million","billion","trillion","quadrillion")
digits<-rev(as.numeric(strsplit(as.character(x),"")[[1]]))
if(is.numeric(x)) {
cat("illion =",illion,"\n")
digilen<-length(digits)
cat("digilen =",digilen,"\n")
if(digilen == 1) digitext<-units[digits[1]]
if(digilen > 1) {
if(digits[2] == 1) digitext<-c(teens[digits[1]+1])
else digitext<-c(tens[digits[2]],units[digits[1]])
}
if(digilen > 2 && digits[3] > 0)
digitext<-c(units[digits[3]],"hundred",digitext)
if(digilen > 3)
digitext<-c(digits2text(x%/%1000,illion+1),digitext)
cat("digitext =",digitext,"\n")
print(digits[1:3])
if(sum(digits[1:3],na.rm=TRUE) > 0)
return(paste(c(digitext,illions[illion]),sep="",collapse=" "))
else
return(paste(digitext,sep="",collapse=" "))
}
else cat("That wasn't a number, Alberto!\n")
}
Jim
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20080304/554b1bd7/attachment.pl