[R] data.frame as argument to data.frame

Jim Rogers jrogers at cantatapharm.com
Wed Oct 30 16:55:49 CET 2002


Hello, 

Documentation for data.frame says, 

"If a list or data frame or matrix is passed to data.frame it is as if
each column had been passed as a separate argument, with the exception
of matrices of class model.matrix. ".

This is not quite true:

> data.frame(a = 1:2, b = 1:4, c = 1:8) # replicates vectors as
necessary
  a b c
1 1 1 1
2 2 2 2
3 1 3 3
4 2 4 4
5 1 1 5
6 2 2 6
7 1 3 7
8 2 4 8

but if one tries to combine a vector with a data.frame that needs to be
replicated, one gets:

> data.frame(data.frame(a = 1:2, b = 1:4), c = 1:8)
Error in data.frame(data.frame(a = 1:2, b = 1:4), c = 1:8) : 
	arguments imply differing number of rows: 4, 8

On the other hand, if one combines a data.frame with a vector that needs
to be replicated, it works:

> data.frame(data.frame(a = 1:8, b = 1:4), c = 1:2)
  a b c
1 1 1 1
2 2 2 2
3 3 3 1
4 4 4 2
5 5 1 1
6 6 2 2
7 7 3 1
8 8 4 2

Is the behavior my second example intended? It would seem more
convenient if the rows of the data.frame were replicated as needed. If
it is intended behavior, a word about this in the documentation might be
helpful. 

> version
         _              
platform i386-pc-mingw32
arch     i386           
os       mingw32        
system   i386, mingw32  
status                  
major    1              
minor    5.1            
year     2002           
month    06             
day      17             
language R    

Thanks,
Jim Rogers

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