[R] convert factor to numeric
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Wed Jun 4 15:42:01 CEST 2003
Philipp Pagel <p.pagel at gsf.de> writes:
> But I just can't believe there is no R function to do this in a more
> readable way. Actually, the behaviour of as.numeric() doesn't strike me
> as very intuitive. I'm sure it has been implemented that way for a
> reason - but what is it?
One reason is S compatibility, as Brian pointed out. But there is also
the point that you can *always* convert a factor to its underlying
integer values, but only *sometimes* convert the level names. Generally
we prefer code that fails more rarely...
as.numeric(as.character(f)) works too, although not quite as
efficiently as as.numeric(levels(f))[f].
--
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