[R] extract the digits of a number

Christos Hatzis christos.hatzis at nuverabio.com
Tue Dec 9 19:11:19 CET 2008


An alternative that works for any base (other than 10) is the following:

> library(sfsmisc)
> digitsBase(1001, 10)
Class 'basedInt'(base = 10) [1:1]
     [,1]
[1,]    1
[2,]    0
[3,]    0
[4,]    1

-Christos

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Gustavo Carvalho
> Sent: Tuesday, December 09, 2008 12:49 PM
> To: r-help at r-project.org
> Subject: [R] extract the digits of a number
> 
> Hello,
> 
> Anyone knows how can I do this in a cleaner way?
> 
> mynumber = 1001
> as.numeric(unlist(strsplit(as.character(mynumber),"")))
> [1] 1 0 0 1
> 
> Thanks in advance,
> 
> Gustavo
> 
> ______________________________________________
> 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.
> 
>



More information about the R-help mailing list