[R] How do I coerce numeric factor columns of data frame to vector?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Sep 9 18:16:50 CEST 2003


Martin Maechler <maechler at stat.math.ethz.ch> writes:

>     >> as.numeric (levels (y)[as.numeric (y)])
>     NaLi> [1] 1 3 1
> 
>     NaLi> which is a bit awkward.

> as.numeric(as.character(y)) !
> 
> {in some cases you might consider using as.integer() instead of as.numeric()}

Better to convert first and then index:

as.numeric(levels(y))[y] 

The speed gain is only going to be visible on huge vectors, though.

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




More information about the R-help mailing list