[R] rbind and data.frame

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Dec 5 11:50:50 CET 2001


Göran Broström <gb at stat.umu.se> writes:

> Version 1.3.1  (2001-08-31) (RH 7.2):
> 
> > dat <- data.frame(x = 1, y = 2)
> > x <- matrix(0, ncol = 2, nrow = 2)
> > x
>      [,1] [,2]
> [1,]    0    0
> [2,]    0    0
> > dat
>   x y
> 1 1 2
> > rbind(dat, x)
>   x y
> 1 1 2
> 2 0 0
> 
> I expected
> 
> > rbind(dat, x)
>   x y
> 1 1 2
> 2 0 0
> 3 0 0
> 
> Is my expectation wrong?

Maybe and maybe not... The help page has

     If you want to combine other objects with data frames, it may be
     necessary to coerce them to data frames first.

and the internal logic in rbind.data.frame is that everything that is
not a data frame or a list is a vector. I'm slightly puzzled as to why
we don't check the lengths but quietly truncate or recycle (whereas
the corresponding cbind code does protest even though we don't always
want it to...)

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