I am not sure if I should call this a bug or just a design imperfection. I have noticed that the data.frame function does not preserve the integrity of multivariate components. Here is a simple illustration:> x <- matrix(1:6,3,2) > y <- 1:3 > z <- data.frame(x=x,y=y) > z$x# NULL> z$y# [1] 1 2 3 One can however get by that by using model.frame:> zz <- model.frame(~x+y) > zz$x# [,1] [,2] # [1,] 1 4 # [2,] 2 5 # [3,] 3 6 I was expecting data.frame to behave like this. I am developing some multivariate nonparametric modeling tools using smoothing splines, with syntax similar to lm and glm. In a formula say y~x1*x2, x1 or x2 or both can be multivariate. The corresponding predict function also has syntax similar to that of lm. To prepare newdata to be used in predict(object,newdata,...), I can use> newdata <- data.frame(x1=newx1,x2=newx2)only for univariate x1 and x2. For multivariate predictors, I have to fake it by using> newdata <- model.frame(~x1+x2,list(x1=newx1,x2=newx2))Chong Gu -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Chong Gu <chong at stat.purdue.edu> writes:> I am not sure if I should call this a bug or just a design > imperfection. > > I have noticed that the data.frame function does not preserve the > integrity of multivariate components. Here is a simple illustration: > > > x <- matrix(1:6,3,2) > > y <- 1:3 > > z <- data.frame(x=x,y=y) > > z$x > # NULL > > z$y > # [1] 1 2 3 > > One can however get by that by using model.frame: > > > zz <- model.frame(~x+y) > > zz$x > # [,1] [,2] > # [1,] 1 4 > # [2,] 2 5 > # [3,] 3 6 > > I was expecting data.frame to behave like this.You're not the first... This apparently goes back to a design decision that data.frame be used to coerce matrices to data frames. You can get the desired behaviour with the I() function:> z <- data.frame(x=I(x),y=y) > z$x[,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 attr(,"class") [1] "AsIs" -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._