Re: [R] hello

From: <rolf_at_math.unb.ca>
Date: Thu, 19 Apr 2007 11:17:07 -0300 (ADT)


francois.morneau_at_ifn.fr wrote:

> ?as.numeric

in response to

> ok how can I do to cancel le function factor for the moment ican't do > this and I'm stuck with my problem for exemple

        .......

        <snip>

        The use of as.numeric() to convert a factor to a numeric
        vector is fraught with peril.

        E.g.


> f <- factor(rep(2*(1:5),3))
> f
[1] 2 4 6 8 10 2 4 6 8 10 2 4 6 8 10 Levels: 2 4 6 8 10
> as.numeric(f)
[1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 which is probably not what one was expecting or wanting. However you *can* get what you want:
> as.numeric(as.character(f))
[1] 2 4 6 8 10 2 4 6 8 10 2 4 6 8 10 cheers, Rolf Turner rolf_at_math.unb.ca

P.S. To save myself key-strokes I have written a wee function

        f2n <- function(x){as.numeric(as.character(x))}

                                        R. T.

______________________________________________
R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 19 Apr 2007 - 14:37:03 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 19 Apr 2007 - 15:00:55 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.