[R] How do I reverse the digits of a number

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Sun Oct 11 14:09:03 CEST 2009


On Sun, Oct 11, 2009 at 12:53 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Try this:
>
>> library(tcltk)
>> as.numeric(tcl("string", "reverse", 123))
> [1] 321

The bit where the original poster said 'unknown length' worried me:

 > as.numeric(tcl("string", "reverse", 12377656534))
[1] 0.4356568

 > as.numeric(paste(rev(strsplit(as.character(1234567890123727723),"")[[1]]),collapse=""))
[1] 6.167273e+18

As well as the use of the word 'number' - both solutions give NA for
negative integers and various things for decimals...

Just a bit of Sunday morning pedantry for you :)

Barry




More information about the R-help mailing list