[R] Converting numbers into words

Thomas Levine thomas.levine at gmail.com
Sun Dec 5 17:50:56 CET 2010


Example data

desk=data.frame(
deskchoice=c('mid','mid','left','bookdrop','mid','bookdrop')
)

--

I like doing stuff like the line below, especially when I'm using Sweave.

print(paste('Within the observation period,',nrow(desk),
'patrons approached the circulation desk.'))


--

But what if I want to put it at the beginning of a sentence?

print(sum(desk$deskchoice=='bookdrop'),'persons',
'used the book drop. Everyone else interacted with a staff member.')

Is there a pretty way to change the result of
sum(desk$deskchoice=='bookdrop')
from "2" to "Two"?

--

And what if the number is one?

print(sum(desk$deskchoice=='bookdrop'),
 c('person','persons')[as.numeric(sum(desk$deskchoice=='bookdrop')!=1)+1],
'used the book drop. Everyone else interacted with a staff member.')

Is there a prettier way of choosing between "person" and "persons"?

--

Thanks
Tom



More information about the R-help mailing list