[R] data frame component replacement: feature or bug?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Sep 6 00:03:29 CEST 1999


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 object

Well, 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list