A non-text attachment was scrubbed... Name: not available Type: text Size: 1262 bytes Desc: not available Url : https://stat.ethz.ch/pipermail/r-help/attachments/19980602/3cf08662/attachment.pl
Jim Lindsey <jlindsey at luc.ac.be> writes:> If an MS-DOS file with CRs still in place is (accidently) read into > R using scan(), an extra zero is added to each line. This can be very > embarassing!Are you blaming us or yourself, then? ;) This looks fairly easy to fix.> > In manipulating repeated measurements data, I generally require > matrices. Unfortunately, matrix factors create a lot of problems. > Consider the following simple examples:This has been changed for 0.62, but I'm afraid not in the direction you'd want, but in the direction of S-plus compatibility. Basically, matrix factors don't exist anymore:> > x <- as.factor(c("F","N",NA)) > > y <- cbind(x,x) > > y[,1] > Error: length of "levels" vector and number of levels differ > > attributes(y)$levels <- NULL > > is.factor(y) > TRUE > > y[,1] > F N NAR> x <- as.factor(c("F","N",NA)) R> y <- cbind(x,x) R> y[,1] [1] 1 2 NA R> is.factor(y) [1] FALSE> > rbind(NULL,gl(2,1,2)) > Error in rbind(NULL,gl(2,1,2)): incompatible factorsR> rbind(NULL,gl(2,1,2)) [,1] [,2] [1,] 1 2> > 3. To obtain the codes of a factor variable, one uses as.numeric(). > However, this function destroys the properties of the matrix, turning > it into a vector.Well...now it also works the other way...: R> y [,1] [,2] [1,] 1 1 [2,] 2 2 [3,] NA NA R> as.factor(y) [1] 1 2 NA 1 2 NA Levels: 1 2 R> factor(y) [1] 1 2 NA 1 2 NA Levels: 1 2 Actually, a factor can have the dim attribute, but then it doesn't show up, except in matrix ops and apply where there's an implicit conversion to integer: R> r [,1] [,2] [1,] 1 3 [2,] 2 4 R> f<-factor(r) R> f [1] 1 2 3 4 Levels: 1 2 3 4 R> dim(f)<-dim(r) R> f [1] 1 2 3 4 Levels: 1 2 3 4 R> f%*%r [,1] [,2] [1,] 7 15 [2,] 10 22 All this is exactly the same in Splus. -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Martin Maechler
1998-Jun-02 10:02 UTC
"as.numeric" / "As.numeric" [was "R-beta: bugs and problems"]
>>>>> "Jim" == Jim Lindsey <jlindsey at luc.ac.be> writes:Jim> ..... Jim> 3. To obtain the codes of a factor variable, one uses Jim> as.numeric(). However, this function destroys the properties of Jim> the matrix, turning it into a vector. as.numeric(.) behaves so consistently (also in S-plus); it has been a `pain' in other situations, such as passing a numeric matrix to C or Fortran and wanting the result to still be a matrix. The ``well-known'' (:-) solution is to use storage.mode(x) <- "numeric" instead, which is rather clumy, especially when passing arguments to .C(..). I've seen As.numeric <- function(x) { storage.mode(x) <- "numeric"; x } solving the above problem. What do you (R-help readers !) think? Should we add this (and corresponding As.integer, As.character, ....) to R ? Or rather use a different function name? However, not "as.numeric" itself! For such a basic function, we usually don't want to be unnecessarily incompatible. User functions written for both R and S should get the same thing for as.numeric(.). Martin Maechler <maechler at stat.math.ethz.ch> <>< Seminar fuer Statistik, ETH-Zentrum SOL G1; Sonneggstr.33 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1086 http://www.stat.math.ethz.ch/~maechler/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 2 Jun 1998, Jim Lindsey wrote:> > 3. To obtain the codes of a factor variable, one uses as.numeric(). > However, this function destroys the properties of the matrix, turning > it into a vector. > Factors are very useful, but they are presently a mess for anything > except simple applications in lm/glm.You can use codes() to extract the codes of a factor variable while keeping the matrix structure R> matrix(factor(1:10),2,5)->a R> codes(a) [,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10 The other problems need more fixing. Thomas Lumley UW Biostatistics -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._