Skip to content

convert factor to numeric

4 messages · Philipp Pagel, Brian Ripley, Peter Dalgaard +1 more

#
Hi R-experts!

Every once in a while I need to convert a factor to a vector of numeric
values. as.numeric(myfactor) of course returns a nice numeric vector of
the indexes of the levels which is usually not what I had in mind:
[1] 25   3.78 16.5 37   109
Levels: 3.78 16.5 25 37 109
[1] 3 1 2 4 5
What I really want is a function "unfactor" that returns v:
[1]  25.00   3.78  16.50  37.00 109.00

Of course I could use something like
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?

cu
	Philipp
#
See the FAQ, Q7.12.
On Wed, 4 Jun 2003, Philipp Pagel wrote:

            
...

It's done that way because that is how it is defined to work in S, and 
lots of code relies on it.
#
Philipp Pagel <p.pagel at gsf.de> writes:
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].
#
On Wed, 4 Jun 2003, Prof Brian Ripley wrote:

            
There's also a consistency problem.  Suppose you have a factor
with levels(*)
   3163 2006 98195 98103  OX13DP

You really don't want to define as.numeric() so that its value for the
first four of these depends on whether the fifth is present.  So either
as.numeric() has to be NA for anything that can't be coerced to a number
or it needs something like its current definition.


	-thomas


(*) I'm not just being difficult.  These are postcodes I have had over the
past ten years or so.