[R] problem with rbind on data.frames that contain data.frames

Michael Lachmann lachmann at eva.mpg.de
Wed Jun 30 21:46:38 CEST 2010


It took me some time to find this bug in my code. Is this a feature of  
R? Am I doing something wrong?

 > a=data.frame(x=1:10,y=1:10)
 > b=data.frame(x=11:20,y=11:20)
 > z=data.frame(1:10,11:20)

 > a$z=z
 > b$z=z

 > rbind(a,b)

Error in `row.names<-.data.frame`(`*tmp*`, value = c("1", "2", "3",  
"4",  :
   duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': ‘1’, ‘10’, ‘2’, ‘3’, ‘4’,  
‘5’, ‘6’, ‘7’, ‘8’, ‘9’

adding rownames to a and b doesn't help:
 > rownames(a)=1:10
 > rownames(b)=11:20
 > rbind(a,b)
Error in `row.names<-.data.frame`(`*tmp*`, value = c("1", "2", "3",  
"4",  :
   duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': ‘1’, ‘10’, ‘2’, ‘3’, ‘4’,  
‘5’, ‘6’, ‘7’, ‘8’, ‘9’

the problem is with the rownames of a$z and b$z...
 > rownames(a$z)=1:10
 > rownames(b$z)=11:20
 > rbind(a,b)
     x  y z.X1.10 z.X11.20
1   1  1       1       11
2   2  2       2       12
3   3  3       3       13
4   4  4       4       14
5   5  5       5       15
6   6  6       6       16
7   7  7       7       17
8   8  8       8       18
9   9  9       9       19
10 10 10      10       20
11 11 11       1       11
12 12 12       2       12
13 13 13       3       13
14 14 14       4       14
15 15 15       5       15
16 16 16       6       16
17 17 17       7       17
18 18 18       8       18
19 19 19       9       19
20 20 20      10       20

I was creating data.frames with data.frame members when I was doing  
computations on the data.frames.
Something like:
 > a=data.frame(x=1:10,y=1:10)
 > b=data.frame(x=11:20,y=11:20)
 > a$z=a*2
 > b$z=b*2
 > rbind(a,b)
Error in `row.names<-.data.frame`(`*tmp*`, value = c("1", "2", "3",  
"4",  :
   duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': ‘1’, ‘10’, ‘2’, ‘3’, ‘4’,  
‘5’, ‘6’, ‘7’, ‘8’, ‘9’


Thanks for listening,
Michael


More information about the R-help mailing list