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
Try this also: library(gsubfn) strapply(as.character(mynumber), "[0-9]", simplify = as.numeric) On Tue, Dec 9, 2008 at 3:48 PM, Gustavo Carvalho <gustavo.bio+R@gmail.com<gustavo.bio%2BR@gmail.com>> wrote:> 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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
I don't know if this is cleaner or not, but here is another way:> mynumber <- 1001 > floor( mynumber/(10^(nchar(mynumber):1 -1))) %% 10[1] 1 0 0 1> mynumber <- 12345678 > floor( mynumber/(10^(nchar(mynumber):1 -1))) %% 10[1] 1 2 3 4 5 6 7 8> mynumber <- 9753086421 > floor( mynumber/(10^(nchar(mynumber):1 -1))) %% 10[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> -----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 10:49 AM > 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.
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. > >
Actually what you have is not so bad. Here is a variation which has one fewer nested (...)> as.numeric(strsplit(as.character(mynumber),"")[[1]])[1] 1 0 0 1> # or > # Try strapply in gsubfn:> library(gsubfn) > mynmber <- 1001 > strapply(as.character(mynumber), ".", as.numeric)[[1]][1] 1 0 0 1 On Tue, Dec 9, 2008 at 12:48 PM, Gustavo Carvalho <gustavo.bio+R at gmail.com> wrote:> 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. >