[R] rbind and data.frame

Stephane Dray dray at biomserv.univ-lyon1.fr
Wed Dec 5 13:26:20 CET 2001


I think that the problem  is that you want to bind a matrix and a 
data frame. You must transform your dataframe in a matrix (or the 
matrix in dataframe) :
>  rbind(as.matrix(dat),x)
   V1 V2
1  1  2
2  0  0
3  0  0
>  xdat_as.data.frame(x)
>  names(x)_names(dat)
>  rbind(dat,x)
   x y
1 1 2
2 0 0
3 0 0

If you bind the matrix and the dataframe, only the first column of 
the matrix is added as a row ... and i don't know (and understand) 
why !!

>  y_matrix(c(3,4,5,6),2,2)
>  y
      [,1] [,2]
[1,]    3    5
[2,]    4    6
>  rbind(dat,y)
   x y
1 1 2
2 3 4
>

>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?
>
>Göran Broström
>
>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
>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
>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
Stéphane DRAY
---------------------------------------------------------------
Biométrie et Biologie évolutive - Equipe "Écologie Statistique"
Universite Lyon 1 - Bat 711 - 69622 Villeurbanne CEDEX - France

Tel : 04 72 43 27 56			   Fax : 04 78 89 27 19
       04 72 43 27 57 	   E-mail : dray at biomserv.univ-lyon1.fr 
---------------------------------------------------------------
ADE-4               http://pbil.univ-lyon1.fr/ADE-4/ADE-4F.html
---------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://stat.ethz.ch/pipermail/r-help/attachments/20011205/fe10b8ce/attachment.html


More information about the R-help mailing list