[R] coercing factors to matrix() --> num/char ? --
Z. Todd Taylor
Todd.Taylor at pnl.gov
Fri Jun 25 17:28:42 CEST 1999
John Thaden <jjthaden at flash.net> wrote regarding factors:
> 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.
I think this is a clear example of when you *don't* want your
factors acting like integers. The underlying integer codes are
almost never meaningful. (Indexing into another object is one
exception recently cited by several people here. But I say even
that use violates the encapsulation of factor objects, placing
external reliance on an internal implementation that,
theoretically, could be subject to change.)
> > lm(y ~ x1 * x2, data=mydata, subset = Exp == 3 | Exp == 5,
> + weights = 5.5 - as.integer(as.character(Exp)))
If factors acted like characters instead of integers, you'd only
have to say "5.5 - as.integer(Exp)". And in other situations
you might need as.numeric(Exp) instead (e.g., your levels are "0",
"1", "1.5", "2.8", ...)
I get so tired of typing as.numeric(as.character(a.factor)).
And if I'm not typing that, I'm typing the plain
as.character(a.factor). It's frustrating that you almost always
have to coerce factors to character before you can do anything
with them.
--Todd
--
Z. Todd Taylor
Pacific Northwest National Laboratory
Todd.Taylor at pnl.gov
Why are they called 'long shorts'?
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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