[R] FAQ and Answer: How to convert factors back to integers

Prof Brian D Ripley ripley at stats.ox.ac.uk
Tue Feb 15 08:13:42 CET 2000


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list