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

Matthew Wiener mcw at ln.nimh.nih.gov
Sun Sep 5 23:08:28 CEST 1999


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))

> t1
          X1 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 <- 2

Note 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list