Rev 27928 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{cbind}\alias{cbind}%%--do C-internal method dispatching !!\alias{rbind}\alias{cbind.data.frame}\alias{rbind.data.frame}\title{Combine R Objects by Rows or Columns}\description{Take a sequence of vector, matrix or data frames arguments and combineby \emph{c}olumns or \emph{r}ows, respectively. These are genericfunctions with methods for other \R classes.}\usage{cbind(\dots, deparse.level = 1)rbind(\dots, deparse.level = 1)}\arguments{\item{\dots}{vectors or matrices. These can be given as named arguments.}\item{deparse.level}{integer controlling the construction of labels;currently, \code{1} is the only possible value.}}\details{The functions \code{cbind} and \code{rbind} are generic, with methodsfor data frames. The data frame method will be used if an argument isa data frame and the rest are vectors or matrices. There can be othermethods; in particular, there is one for time series objects.Data frames can be \code{cbind}-ed with matrices, in which caseeach matrix forms a single column in the result, unlike calling\code{\link{data.frame}}.The \code{rbind} data frame method takes the classes of the columnsfrom the firstdata frame. Factors have their levels expanded as necessary (inthe order of the levels of the levelsets of the factors encountered)and the result is an ordered factor if and only if all the componentswere ordered factors. (The last point differs from S-PLUS.)If there are several matrix arguments, they must all have the samenumber of columns (or rows) and this will be the number of columns (orrows) of the result. If all the arguments are vectors, the number ofcolumns (rows) in the result is equal to the length of the longestvector. Values in shorter arguments are recycled to achieve thislength (with a \code{\link{warning}} if they are recycled only\emph{fractionally}).When the arguments consist of a mix of matrices and vectors the numberof columns (rows) of the result is determined by the number of columns(rows) of the matrix arguments. Any vectors have their valuesrecycled or subsetted to achieve this length.For \code{cbind} (\code{rbind}), vectors of zero length (including\code{NULL}) are ignored unless the result would have zero rows(columns), for S compatibility.(Zero-extent matrices do not occur in S3 and are not ignored in \R.)}\value{A matrix or data frame combining the \code{\dots} argumentscolumn-wise or row-wise.For \code{cbind} (\code{rbind}) the column (row) names are taken fromthe names of the arguments, or where those are not supplied bydeparsing the expressions given (if that gives a sensible name).The names will depend on whether data frames are included:see the examples.}\note{The method dispatching is \emph{not} done via\code{\link{UseMethod}()}, but by C-internal dispatching.Therefore, there is no need for, e.g., \code{rbind.default}.The dispatch algorithm is described in the source file(\file{.../src/main/bind.c}) as\enumerate{\item For each argument we get the list of possible classmemberships from the class attribute.\item We inspect each class in turn to see if there is anan applicable method.\item If we find an applicable method we make sure that it isidentical to any method determined for prior arguments.If it is identical, we proceed, otherwise we immediatelydrop through to the default code.}If you want to combine other objects with data frames, it may benecessary to coerce them to data frames first. (Note that thisalgorithm can result in calling the data frame method if thearguments are all either data frames or vectors, and this will result inthe coercion of character vectors to factors.)}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth \& Brooks/Cole.}\seealso{\code{\link{c}} to combine vectors (and lists) as vectors,\code{\link{data.frame}} to combine vectors and matrices as a dataframe.}\examples{cbind(1, 1:7) # the '1' (= shorter vector) is recycledcbind(1:7, diag(3))# vector is subset -> warningcbind(0, rbind(1, 1:3))cbind(I=0, X=rbind(a=1, b=1:3)) # use some namesxx <- data.frame(I=rep(0,2))cbind(xx, X=rbind(a=1, b=1:3)) # named differentlycbind(0, matrix(1, nrow=0, ncol=4))#> Warning (making sense)dim(cbind(0, matrix(1, nrow=2, ncol=0)))#-> 2 x 1}\keyword{array}\keyword{manip}