[R] convert factor to numeric
Thomas Lumley
tlumley at u.washington.edu
Wed Jun 4 17:24:35 CEST 2003
On Wed, 4 Jun 2003, Prof Brian Ripley wrote:
> See the FAQ, Q7.12.
>
> On Wed, 4 Jun 2003, Philipp Pagel wrote:
>
> > 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:
> ...
>
> It's done that way because that is how it is defined to work in S, and
> lots of code relies on it.
>
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.
More information about the R-help
mailing list