[R] factor issue

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Thu Apr 30 14:14:33 CEST 2009


check the following:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-do-I-convert-factors-to-numeric_003f


Best,
Dimitris


Nattu wrote:
> Hi,
> 
> I have a column which I have to convert from factors to numeric. I am using
> the command as.numeric(variable) to do this. But when I convert the numeric
> value becomes different from factor . for example.
> 
>> class(B)
> [1] "factor"
>> B
>  [1] 180  <NA> 183  175  163  155  <NA> 188  191  160  170  165  152  170
> 165
> [16] 163  160  163  165  <NA>
>> as.numeric(B)
>  [1] 46 NA 47 42 30 26 NA 49 50 29 37 32 24 37 32 30 29 30 32 NA
> Is there a different way to convert factor to respective number ?
> 
> 
> also when i try to unclass it i get following output
> 
>> unclass(B)
>  [1] 46 NA 47 42 30 26 NA 49 50 29 37 32 24 37 32 30 29 30 32 NA
> attr(,"levels")
>  [1] "102" "104" "107" "109" "110" "112" "114" "117" "119" "122" "124" "125"
> [13] "127" "129" "130" "132" "135" "137" "140" "142" "145" "147" "150" "152"
> [25] "153" "155" "157" "159" "160" "163" "164" "165" "166" "167" "168" "169"
> [37] "170" "171" "172" "173" "174" "175" "177" "178" "179" "180" "183" "185"
> [49] "188" "191" "193" "196" "198" "201" "203" "211" "46"  "48"  "51"  "56"
> [61] "58"  "60"  "61"  "63"  "64"  "65"  "66"  "69"  "71"  "74"  "76"  "79"
> [73] "81"  "84"  "86"  "89"  "91"  "94"  "97"  "99"  "U"
> 
> Any help will be appreciated.
> 
> Thanks a lot.
> Regards,
> Nataraju
> GM
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list