Martin Maechler
1999-Jun-23 08:02 UTC
[R] coercing factors to matrix() --> num/char ? -- inconsistencies|?
The old factor() wars...... {{maybe don't just report that factor()s are broken by design..}} If ff is a factor, e.g., ff <- as.factor(rep(1:2,3)) f2 <- ff; levels(f2) <- c("Lo","Hi") 1) we don't allow {in R as in S-plus 3.x} arithmetic on factors, i.e. 1 + ff gives an error, and I think most of us believe this is as desired. {{if one really wants the factor codes, use codes(ff) ...}}. [BTW: S-plus 5.0r3 doesn't give an error anymore but silently returns NA, i.e., rep(NA,6) for the above example ] 2) On the other hand, it seems that implicit coercion to character() is rather ok, such as > paste("A", f2) [1] "A Lo" "A Hi" "A Lo" "A Hi" "A Lo" "A Hi" -------->From this logic, coercing to a matrix should coerce to character ratherthan integer/numeric codes. However, A: both R and S-plus 3.4 give an INTEGER matrix (with codes(.)) for rbind(ff) cbind(f2) B: R gives an integer matrix but S-plus a character one for matrix(ff) or matrix(f2) ------- So far the current behavior which I don't like. I believe all matrix coercions should return mode "character" objects for consistency with other coercion behavior of factors. S(-plus) seems particularly inconsistent in doing different coercions for cbind() and matrix(). Opinions ? Proposals ? Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum SOL G1; Sonneggstr.33 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1086 <>< -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 10:17 UTC
[R] coercing factors to matrix() --> num/char ? -- inconsistencies|?
Martin Maechler <maechler at stat.math.ethz.ch> writes: [On R-help, not -devel??]> The old factor() wars...... > {{maybe don't just report that factor()s are broken by design..}}...> 1) we don't allow {in R as in S-plus 3.x} arithmetic on factors, i.e. > 1 + ff > gives an error, and I think most of us believe this is as desired. > {{if one really wants the factor codes, use codes(ff) ...}}.As.integer(), you mean. Codes() has some "portable brain damage" associated with it. We do however have indexing with implicit integer coercion, e.g.> ff<-factor(1:3,labels=3:1) > letters[ff][1] "a" "b" "c"> >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.> A: both R and S-plus 3.4 give an INTEGER matrix (with codes(.)) for > > rbind(ff) > cbind(f2) > > B: R gives an integer matrix but S-plus a character one for > > matrix(ff)...> So far the current behavior which I don't like. > I believe all matrix coercions should return mode "character" objects > for consistency with other coercion behavior of factors. > > S(-plus) seems particularly inconsistent in doing different coercions > for cbind() and matrix(). > > Opinions ? Proposals ?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... In what kinds of code would this become relevant? BTW, we also have this little item:> matrix(ff)[,1] [1,] 1 [2,] 2 [3,] 3> array(ff,c(3,1))[,1] [1,] "3" [2,] "2" [3,] "1" (In Splus, matrix() *calls* array() so this won't happen) -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._