[R] coercing factors to matrix() --> num/char ? --

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jun 25 16:59:39 CEST 1999


> Date: Fri, 25 Jun 1999 08:56:50 -0500
> To: "Z. Todd Taylor" <Todd.Taylor at pnl.gov>

I think here you wanted as.numeric, not as.integer, as you wanted a 
number, not an S object of storage mode "integer". A small point, of
course.

Some people think as.numeric(factor) should coerce "1" to 1, and it
once did in R. But this broke far too much. If this is what you want,
you need as.numeric(as.character(factor)), as in

> tt <- factor(c(as.character(0:3), "extra"))
> as.numeric(tt)
[1] 1 2 3 4 5
> as.numeric(as.character(tt))
Warning: NAs introduced by coercion
[1]  0  1  2  3 NA

Your problem is that that you are not using the  number' in 5.5 - Exp,
but some other attribute of the experiment, and you need to set it up
explicitly.  If you remember that as.numeric(factor) gives what one
would naively expect codes(factor) to give, you should not get _too_
confused.  (I think codes() must be seen as a mistake in the carry-over
from categories to factors in ca 1991.)

> Date: Fri, 25 Jun 1999 08:56:50 -0500
> To: "Z. Todd Taylor" <Todd.Taylor at pnl.gov>
> 
> Peter Dalgaard:
> >> Hm. I'm not sure there really is much logic in this... Factors are
> >> sometimes character-like, sometimes integer-like.
> 
> Z. Todd Taylor:
> >I'd be interested to hear a discussion of when they need to
> >behave like integers.
> 
> Yesterday, using S+ 4.5 (Win), for instance:
> 
> #Exp is factor containing experiment numbers
> >levels(mydata$Exp)
>  [1] "0"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
>  [14] "13" "14" "15" "16" "17" "18" "19"
> 
> >lm(y ~ x1 * x2, data=mydata, subset = Exp == 3 | Exp == 5,
> +  weights = 5.5 - Exp)
> Error in Ops.factor(5.5, Exp): "-" not meaningful for factors
> Dumped
> 
> >lm(y ~ x1 * x2, data=mydata, subset = Exp == 3 | Exp == 5,
> +  weights = 5.5 - as.integer(Exp) )
> Error in lm.wfit(X, Y, weights, method): negative weights not allowed
> Dumped
> # . . . because as.integer(Exp) gives the codes as integers, not the
> values, and
> # the code for Exp 5 is 6.
> 
> > lm(y ~ x1 * x2, data=mydata, subset = Exp == 3 | Exp == 5,
> +   weights = 5.5 - as.integer(as.character(Exp)))
> 
> Call: 
> lm(formula = y ~ x1 * x2, data = mydata,
> #. . . Remainder of regression report truncated.
> 
> Z.T.T.:
> >I can see the need to access the integers for
> >"lower level" programming, for example, but when else?
> 
> ALL of my programming is "lower level"!  ;-)
> ************************************************************
> John J. Thaden, Ph.D., Instructor        jjthaden at life.uams.edu
> Department of Geriatrics                     (501) 257-5583
> University of Arkansas for Medical Sciences  FAX: (501) 257-4822
>       mail & ship to:	J. L. McClellan V.A. Medical Center
> 		Research-151 (Room GB103 or GC124)
> 		4300 West 7th Street
> 		Little Rock AR 72205 USA
> ***********************************************************

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list