Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk> wrote:> Martin Maechler <maechler at stat.math.ethz.ch> writes:[ snip... ]> > >From this logic, coercing to a matrix should coerce to character rather > > than integer/numeric codes. > > Hm. I'm not sure there really is much logic in this... Factors are > sometimes character-like, sometimes integer-like.I'd be interested to hear a discussion of when they need to behave like integers. In my work I can't ever remember needing anything other than the character representation in my "user level" coding. I can see the need to access the integers for "lower level" programming, for example, but when else? (My thought has always been that factors should *always* coerce to character unless the user explicitly requests otherwise.) [ snip... ]> Not really sure whether the problem is that R is consistent with > itself or inconsistent with S. If we change as you suggest, we'll > have two incompatibilities instead of one...Factors are one area where I deem inconsistency with S a distinct advantage. :-) --Todd -- Z. Todd Taylor Pacific Northwest National Laboratory Todd.Taylor at pnl.gov Why do you say there's one left, when it didn't leave? -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
1999-Jun-23 15:44 UTC
[R] coercing factors to matrix() --> num/char ? --
"Z. Todd Taylor" <Todd.Taylor at pnl.gov> writes:> I'd be interested to hear a discussion of when they need to > behave like integers. In my work I can't ever remember needing > anything other than the character representation in my "user > level" coding. I can see the need to access the integers for > "lower level" programming, for example, but when else? > > (My thought has always been that factors should *always* coerce > to character unless the user explicitly requests otherwise.)The main one is probably indexing:> ff<-factor(rbinom(20,1,.5),levels=0:1) > x<-rnorm(20) > y<-rnorm(20) > plot(x,y,pch=c("f","m")[ff]).which BTW also applies to the cbind issue since you can do things like:> ff<-factor(rbinom(10,1,.5),levels=0:1) > gg<-factor(rbinom(10,1,.5),levels=0:1)> data.frame(ff,gg,comb.code=matrix(1:4,2)[cbind(ff,gg)])ff gg comb.code 1 0 1 3 2 1 0 2 3 1 0 2 4 0 1 3 5 0 1 3 6 0 0 1 7 1 1 4 8 0 0 1 9 1 0 2 10 1 1 4 -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 *********************************************************** -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._