Re: [R] convert factor to numeric

About this list Date view Thread view Subject view Author view Attachment view

From: Peter Dalgaard BSA (p.dalgaard@biostat.ku.dk)
Date: Wed 04 Jun 2003 - 23:49:48 EST


Message-id: <x27k820x6r.fsf@biostat.ku.dk>

Philipp Pagel <p.pagel@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@biostat.ku.dk)             FAX: (+45) 35327907

______________________________________________ R-help@stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Tue 01 Jul 2003 - 09:11:52 EST