Matthew Wiener
1999-Sep-05 21:08 UTC
[R] data frame component replacement: feature or bug?
Hi, all. The following does not behave as I think it should, and as it seems to me it has in the past (although I can't check this easily). I know it happens in both R-0.64.2 and R-0.65.0 on an old Power Computing running Linux-PPC 1999, and in R-0.64.2 on an SGI running Irix 6.5. Try the following: t1 <- data.frame(matrix(rnorm(16), nc=4))> t1X1 X2 X3 X4 1 -0.7206945 0.4511310 -1.0638681 -0.9128938 2 -1.6340557 -0.8063090 0.7753397 0.3607708 3 0.5999834 0.5256372 0.4393342 1.3218485 4 2.8542598 -2.8740136 2.0165816 -0.3026954> dim(t1)[1] 4 4> t1$X1 <- 1 > t1$X2 <- 2Note that R accepts this. I think it's replaced t1$X1 with a vector of 1's using the usual repeating rules, and similarly for X2. So I should still have a 4 X 4 data frame. R seems to think so too.> dim(t1)[1] 4 4> is.data.frame(t1)[1] TRUE But:> print(t1)Error: dim<- length of dims do not match the length of object> t1[, 1:2]$X1 [1] 1 $X2 [1] 2 whereas t1[,3:4] gives me a 4 x 2 data frame as I would expect. It seems that the "listness" of t1 is beating out it's "matrixness" (having dimension and so on). It is possible to get what I want with t1$X1 <- rep(1, length(t1$X1)). As I said before, I thought that t1$X1 <- 1 would replace the whole first column of t1 with 1's. Did other people think so too? And is that in fact the way R should work? (This example used all numeric columns, in which case a matrix would work. In some of my real applications, that's not necessarily practical.) Thanks, Matt Wiener -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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-Sep-05 22:03 UTC
[R] data frame component replacement: feature or bug?
Matthew Wiener <mcw at ln.nimh.nih.gov> writes:> t1 <- data.frame(matrix(rnorm(16), nc=4)) > t1$X1 <- 1 > t1$X2 <- 2 > print(t1) > Error: dim<- length of dims do not match the length of objectWell, it is prototype-compatible. Splus 5.3 does likewise. A way out is t1<-data.frame(unclass(t1)) However, we do seem to have a bug in the area:> t1 <- data.frame(matrix(rnorm(16), nc=4)) > transform(t1,X1=1)Error: dim<- length of dims do not match the length of object> transform(t1,X1=1,X2=2)X1 X2 X3 X4 1 1 2 0.6020524 1.1595935 2 1 2 -1.8212204 -0.2118746 3 1 2 1.5506706 0.0680275 4 1 2 -0.8872958 0.4709870 This boils down to the construction dfr[1]<-list(X1=1) which doesn't extend dfr$X1, whereas dfr[c(1,2)]<-list(X1=3,X2=4) does. Splus handles both cases. -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._