[R] factor to real - best way to convert

Knut Krueger Knut-krueger at einthal.de
Wed May 3 19:01:41 CEST 2006


I have got  factor from read.xls:
 > is(factor_value)
[1] "factor"   "oldClass"

....
[288] -0.32   0.18    0.18    0.18    -0.32   0.18    0.68  
[295] 0.68    0.18  
43 Levels: -0.05 -0.13 -0.15 -0.18 -0.20 -0.26 ... 1.33

If I am using the funciton as.real(factor_value)

I get
............
[271] 17 17  8 22  8  8 17 17 17 17 17 17 17 17 23  7 35  7
[289] 23 23 23  7 23 35 35 23

So I used as.real(as.matrix(factor_value))
The result is as expected:
[271]    NA    NA -0.35  0.15 -0.35 -0.35    NA    NA    NA
[280]    NA    NA    NA    NA    NA  0.18 -0.32  0.68 -0.32
[289]  0.18  0.18  0.18 -0.32  0.18  0.68  0.68  0.18

Ok I found the way to convert with try and error, but I do not understand the way
-------------------------------------------------------------
and I found the hint in the fullref_manual:

x<- as.numeric(levels(factor_value))[factor_value])

Ok much better, but I would not be able to find the way from the 
?as.numeric help page.

Both versions are complete struggled in my mind.

maybe anybody is albe to write some hints for me.



with regards
Knut




More information about the R-help mailing list