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

Gabor Grothendieck ggrothendieck at gmail.com
Sun Oct 11 14:39:41 CEST 2009


On Sun, Oct 11, 2009 at 8:09 AM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> 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

If you really are dealing with numbers this long pass them as strings:
> as.numeric(tcl("string", "reverse", format(12377656534)))
[1] 43565677321

At some length this will fail too but one can use just strings:
> as.character(tcl("string", "reverse", "12377656534"))
[1] "43565677321"




More information about the R-help mailing list