[R] Turn factors to numeric

John Kane jrkrideau at yahoo.ca
Fri Oct 10 16:14:48 CEST 2008


Not an answer but I find that 

DF <- data.frame(let = letters[1:3], num = 1:3, 
                stringsAsFactors = FALSE) 
is very handy.  Damn it, if I want a factor I'll tell the machine I do :)

Or you can also set this option globally with 
 options(stringsAsFactors = TRUE) 

Gabor Grothendieck warns that this last can possibly cause problems when using R programs from other places.  I ran into this problem once with reshape. 




--- On Thu, 10/9/08, Michael Kubovy <kubovy at virginia.edu> wrote:

> From: Michael Kubovy <kubovy at virginia.edu>
> Subject: Re: [R] Turn factors to numeric
> To: "Charilaos Skiadas" <cskiadas at gmail.com>
> Cc: "r-help" <r-help at r-project.org>
> Received: Thursday, October 9, 2008, 8:25 AM
> Dear R-helpers,
> 
> The FAQ in question says:
> 
> > It may happen that when reading numeric data into R
> (usually, when  
> > reading in a file), they come in as factors. If f is
> such a factor  
> > object, you can use
> >      as.numeric(as.character(f))
> > to get the numbers back. More efficient, but harder to
> remember, is
> >
> >      as.numeric(levels(f))[as.integer(f)]
> 
> I wonder why the R Core group did not choose to make such a
> useful  
> operation simpler for the user (i.e., something like a  
> factor2numeric() function that would be a wrapper to the
> more  
> efficient command).
> _____________________________
> Professor Michael Kubovy
> University of Virginia
> Department of Psychology
> USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
> Parcels:    Room 102        Gilmer Hall
>          McCormick Road    Charlottesville, VA 22903
> Office:    B011    +1-434-982-4729
> Lab:        B019    +1-434-982-4751
> Fax:        +1-434-982-4766
> WWW:    http://www.people.virginia.edu/~mk9y/
> 
> On Oct 9, 2008, at 7:16 AM, Charilaos Skiadas wrote:
> 
> > R-FAQ 7.10:
> >
> http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-do-I-convert-factors-to-numeric_003f
> >
> > On Oct 9, 2008, at 6:59 AM, joseph kambeitz wrote:
> >
> >> I am having some problems while trying to fit
> simple data.
> >> I aggregated some data using:
> >> data1 <- aggregate(data1$T2,
> list=(SOA=data1$SOA), mean)
> >>
> >> unfortunatly this coerces my variable SOA into a
> factor. Therefore  
> >> when a afterwards try to fit a simple equation to
> my variable T2  
> >> using a formula on SOA i get a error because SOA
> is a factor and  
> >> that "*" is not meaningful for
> factors...
> >>
> >> nls(T2 ~ a + b*SOA, start=list(a=1,b=1),
> data=data1, trace=TRUE)
> >>
> >> In fact SOA is a numeric variable (in my
> experiments it is the time  
> >> that passed!) so i would like to re-coerce it into
> a numeric  
> >> variable to do the fit or to find a method to do
> the fit even  
> >> though SOA is a factor. Thanks a lot for your
> help!
> >>
> >> Best
> >> Jokel
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list