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

A.J. Rossini rossini at biostat.washington.edu
Mon Feb 14 23:52:09 CET 2000



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



More information about the R-help mailing list