[R] rbind and data.frame

Agustin Lobo alobo at ija.csic.es
Wed Dec 5 14:50:34 CET 2001


Don't mix diffferent types of objects. Use:

> colnames(x) <- names(dat)
> rbind(dat,as.data.frame(x))
  x y
1 1 2
2 0 0
3 0 0

Note that 
> as.matrix(dat)
  x y
1 1 2

hence your result.

Also note that if you do not give to x
the names of dat, you get an error because
of the names of dat:

> x <- matrix(0, ncol = 2, nrow = 2)
> rbind(dat,as.data.frame(x))
Error in match.names(clabs, names(xi)) : names don't match previous names:
         V1, V2

Agus


Dr. Agustin Lobo
Instituto de Ciencias de la Tierra (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona SPAIN
tel 34 93409 5410
fax 34 93411 0012
alobo at ija.csic.es


On Wed, 5 Dec 2001, [iso-8859-1] Göran Broström wrote:

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

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