A.J. Rossini
2000-Feb-14 22:52 UTC
[R] FAQ and Answer: How to convert factors back to integers
(thanks to thomas for assistance; I'm posting this for archival reasons). Question: My silly dataset looks like "00223423", "00234234", ... and when I read it in, I get a factor which looks like and integer. How do I convert it back? (i.e. I've got something like:> hivplasma[1] 00050400 00219000 00136000 00000906 00142000 00002150 00000400 00001880 [9] 00000200 00000400 00115000 00000924 00051500 00000216 00007670 00000200 [17] 00538000 00001720 00000738 00000400 00759000 00023900 00002166 00000200 [25] 00000200 00185000 00002160 00003056 00002770 00002040 00000200 00000200 [33] 00000582 00054898 00003312 00002010 00010365 00000966 00000465 00000200 [41] 00000400 00480731 00000931 00790349 00033402 00000200 00000200 00000400 [49] 00760993 00000649 00000511 01611560 00009509 00001181 00000400 00053308 [57] 00000939 00001229 00006537 00000400 00000400 00141695 00001471 00000490 [65] 00004674 00001095 00000872 00002461 00000400 00000400 00144266 00394994 [73] 00550144 00015851 00000400 00000000 Levels: 00000000 00000200 00000216 00000400 00000465 00000490 00000511 00000582 00000649 00000738 00000872 00000906 00000924 00000931 00000939 00000966 00001095 00001181 00001229 00001471 00001720 00001880 00002010 00002040 00002150 00002160 00002166 00002461 00002770 00003056 00003312 00004674 00006537 00007670 00009509 00010365 00015851 00023900 00033402 00050400 00051500 00053308 00054898 00115000 00136000 00141695 00142000 00144266 00185000 00219000 00394994 00480731 00538000 00550144 00759000 00760993 00790349 01611560>) Answer: hivplasma.integer.NOT.FACTOR <- as.numeric(levels(hivplasma))[codes(hivplasma)] best, -tony -- A.J. Rossini Research Assistant Professor of Biostatistics Biostatistics/Univ. of Washington (Th) Box 357232 206-543-1044 (3286=fax) Center for AIDS Research/HMC/UW (M/F) Box 359931 206-731-3647 (3693=fax) VTN/SCHARP/FHCRC (Tu/W) Box 358080 206-667-7025 (4812=fax) rossini@(biostat.washington.edu|u.washington.edu|hivnet.fhcrc.org) http://www.biostat.washington.edu/~rossini -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2000-Feb-14 23:12 UTC
[R] FAQ and Answer: How to convert factors back to integers
rossini at biostat.washington.edu (A.J. Rossini) writes:> as.numeric(levels(hivplasma))[codes(hivplasma)]^^^^^ Whoops. Make that as.integer(), codes() has some Splus-compatible breakage built into it:> f<-factor(1:10) > f[1] 1 2 3 4 5 6 7 8 9 10 Levels: 1 2 3 4 5 6 7 8 9 10> as.numeric(levels(f))[codes(f)][1] 1 3 4 5 6 7 8 9 10 2 It happens because 10 sorts between 1 and 2, alphabetically. Yes, it is silly... If you're not badly squeezed for space, as.numeric(as.character(f)) works too, and is easier to remember. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Prof Brian D Ripley
2000-Feb-15 07:13 UTC
[R] FAQ and Answer: How to convert factors back to integers
On 14 Feb 2000, A.J. Rossini wrote:> > > (thanks to thomas for assistance; I'm posting this for archival > reasons). > > Question: > > My silly dataset looks like "00223423", "00234234", ... > and when I read it in, I get a factor which looks like and > integer. How do I convert it back? > > (i.e. I've got something like: > > hivplasma > [1] 00050400 00219000 00136000 00000906 00142000 00002150 00000400 00001880 > [9] 00000200 00000400 00115000 00000924 00051500 00000216 00007670 00000200 > [17] 00538000 00001720 00000738 00000400 00759000 00023900 00002166 00000200 > [25] 00000200 00185000 00002160 00003056 00002770 00002040 00000200 00000200 > [33] 00000582 00054898 00003312 00002010 00010365 00000966 00000465 00000200 > [41] 00000400 00480731 00000931 00790349 00033402 00000200 00000200 00000400 > [49] 00760993 00000649 00000511 01611560 00009509 00001181 00000400 00053308 > [57] 00000939 00001229 00006537 00000400 00000400 00141695 00001471 00000490 > [65] 00004674 00001095 00000872 00002461 00000400 00000400 00144266 00394994 > [73] 00550144 00015851 00000400 00000000 > Levels: 00000000 00000200 00000216 00000400 00000465 00000490 00000511 00000582 00000649 00000738 00000872 00000906 00000924 00000931 00000939 00000966 00001095 00001181 00001229 00001471 00001720 00001880 00002010 00002040 00002150 00002160 00002166 00002461 00002770 00003056 00003312 00004674 00006537 00007670 00009509 00010365 00015851 00023900 00033402 00050400 00051500 00053308 00054898 00115000 00136000 00141695 00142000 00144266 00185000 00219000 00394994 00480731 00538000 00550144 00759000 00760993 00790349 01611560 > > > ) > > Answer: > > hivplasma.integer.NOT.FACTOR <- > as.numeric(levels(hivplasma))[codes(hivplasma)]No, please do not do that in general. The answer is as.numeric(as.character(hivplasma)) as your answer depends on the levels being in alphabetical order (which they happen to be). Replace codes by as.numeric if you want to do that sort of thing: `codes' does not give the numeric codes, it re-codes. See its help page for a warning! -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._