Skip to content

extract the digits of a number

5 messages · Gustavo Carvalho, Henrique Dallazuanna, Greg Snow +2 more

#
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
#
I don't know if this is cleaner or not, but here is another way:
[1] 1 0 0 1
[1] 1 2 3 4 5 6 7 8
[1] 9 7 5 3 0 8 6 4 2 1


--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
#
An alternative that works for any base (other than 10) is the following:
Class 'basedInt'(base = 10) [1:1]
     [,1]
[1,]    1
[2,]    0
[3,]    0
[4,]    1

-Christos
#
Actually what you have is not so bad.  Here is a variation
which has one fewer nested (...)
[1] 1 0 0 1
[1] 1 0 0 1

On Tue, Dec 9, 2008 at 12:48 PM, Gustavo Carvalho
<gustavo.bio+R at gmail.com> wrote: