jens.oehlschlaegel-akiyoshi@mdfactory.de
1999-Dec-23 18:17 UTC
data.frame(I(matrix)) ? (PR#388)
I observe dificulties with using data.frame(I(matrix))> mat <- matrix(letters, 2, 2) > dimnames(mat) <- list(c(1:2), c("x","y")) > matx y 1 "a" "c" 2 "b" "d"> dd <- data.frame(I(mat)) > dddI.mat..x I.mat..y 1 a a 2 b b 3 c c doesn't look too bad, but, has only one column name:> dimnames(dd)[[1]] [1] "1" "2" [[2]] [1] "I.mat." and unexpected structure> str(ddd)`data.frame': 3 obs. of 1 variable: $ I.mat.: chr [1:3, 1:2] "a" "b" "c" "a" "b" "c" ..- attr(*, "dimnames")=List of 2 .. ..$ : NULL .. ..$ : chr "x" "y" ..- attr(*, "class")= chr "AsIs">column 1 is a matrix> ddd[,1]x y [1,] "a" "a" [2,] "b" "b" [3,] "c" "c" attr(,"class") [1] "AsIs" This dataframe is not a simple list with each element representing one column. Consequently, e.g.> sapply(ddd, FUN=function(x)x)I.mat. [1,] "a" [2,] "b" [3,] "c" [4,] "a" [5,] "b" [6,] "c" is no longer a matrix. platform Windows arch x86 os Win32 system x86, Win32 status major 0 minor 90.1 year 1999 month December day 15 language R -- Dr. Jens Oehlschlägel-Akiyoshi MD FACTORY GmbH Bayerstrasse 21 80335 München Tel.: 089 545 28-27 Fax.: 089 545 28-10 http://www.mdfactory.de Standard Disclaimers: Opinions expressed here are personal and are not otherwise represented. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel 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-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thu, 23 Dec 1999 jens.oehlschlaegel-akiyoshi@mdfactory.de wrote:> > > I observe dificulties with using data.frame(I(matrix)) > > > mat <- matrix(letters, 2, 2) > > dimnames(mat) <- list(c(1:2), c("x","y")) > > mat > x y > 1 "a" "c" > 2 "b" "d" > > > dd <- data.frame(I(mat)) > > ddd > I.mat..x I.mat..y > 1 a a > 2 b b > 3 c c > > doesn't look too bad, > but, > has only one column name: > > > dimnames(dd) > [[1]] > [1] "1" "2" > > [[2]] > [1] "I.mat." > > and unexpected structure > > str(ddd) > `data.frame': 3 obs. of 1 variable: > $ I.mat.: chr [1:3, 1:2] "a" "b" "c" "a" "b" "c" > ..- attr(*, "dimnames")=List of 2 > .. ..$ : NULL > .. ..$ : chr "x" "y" > ..- attr(*, "class")= chr "AsIs" > > > > column 1 is a matrix > > ddd[,1] > x y > [1,] "a" "a" > [2,] "b" "b" > [3,] "c" "c" > attr(,"class") > [1] "AsIs" > > This dataframe is not a simple list with each element representing one > column. > > Consequently, e.g. > > > sapply(ddd, FUN=function(x)x) > I.mat. > [1,] "a" > [2,] "b" > [3,] "c" > [4,] "a" > [5,] "b" > [6,] "c" >This is the Right Thing: we use the fact that multicolumn things can be a single column of a data frame in logistic regression and extensively in the survival analysis functions (and probably other places). However, it does make it hard to turn a character matrix into a data frame. Perhaps there should be an option of some sort? -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel 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-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._