\HeaderA{cbind}{Combine R Objects by Rows or Columns}{cbind}
\aliasA{.\_\_H\_\_.cbind}{cbind}{..Rul..Rul.H.Rul..Rul..cbind}
\aliasA{.\_\_H\_\_.rbind}{cbind}{..Rul..Rul.H.Rul..Rul..rbind}
\methaliasA{cbind.data.frame}{cbind}{cbind.data.frame}
\aliasA{rbind}{cbind}{rbind}
\methaliasA{rbind.data.frame}{cbind}{rbind.data.frame}
\keyword{array}{cbind}
\keyword{manip}{cbind}
\begin{Description}\relax
Take a sequence of vector, matrix or data frames arguments and combine
by \emph{c}olumns or \emph{r}ows, respectively.  These are generic
functions with methods for other \R{} classes.
\end{Description}
\begin{Usage}
\begin{verbatim}
cbind(..., deparse.level = 1)
rbind(..., deparse.level = 1)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{...}] vectors or matrices.  These can be given as named
arguments.  Other \R{} objects will be coerced as appropriate: see
section Details and Value.  (For the \code{"data.frame"} method of
\code{cbind} these can be further arguments to
\code{\LinkA{data.frame}{data.frame}} such as \code{stringsAsFactors}.)
\item[\code{deparse.level}] integer controlling the construction of labels in
the case of non-matrix-like arguments (for the default method):\\
\code{deparse.level = 0} constructs no labels; the default,
\code{deparse.level = 1 or 2} constructs labels from the argument
names, see the \sQuote{Value} section below.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The functions \code{cbind} and \code{rbind} are S3 generic, with
methods for data frames.  The data frame method will be used if at
least one argument is a data frame and the rest are vectors or
matrices.  There can be other methods; in particular, there is one for
time series objects.  See the section on Dispatch for how the method
to be used is selected.

In the default method, all the vectors/matrices must be atomic (see
\code{\LinkA{vector}{vector}}) or lists.  Expressions are not allowed.
Language objects (such as formulae and calls) and pairlists will be
coerced to lists: other objects (such as names and external pointers)
will be included as elements in a list result.

If there are several matrix arguments, they must all have the same
number of columns (or rows) and this will be the number of columns (or
rows) of the result.  If all the arguments are vectors, the number of
columns (rows) in the result is equal to the length of the longest
vector.  Values in shorter arguments are recycled to achieve this
length (with a \code{\LinkA{warning}{warning}} if they are recycled only
\emph{fractionally}).

When the arguments consist of a mix of matrices and vectors the number
of columns (rows) of the result is determined by the number of columns
(rows) of the matrix arguments.  Any vectors have their values
recycled 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.)
\end{Details}
\begin{Value}
For the default method, a matrix combining the \code{...} arguments
column-wise or row-wise.  (Exception: if there are no inputs or all
the inputs are \code{NULL}, the value is \code{NULL}.)

The type of a matrix result determined from the highest type of any of
the inputs in the hierarchy raw < logical < integer < real < complex <
character < list .

For \code{cbind} (\code{rbind}) the column (row) names are taken from
the \code{colnames} (\code{rownames}) of the arguments if these are
matrix-like.  Otherwise from the names of the arguments or where those
are not supplied and \code{deparse.level > 0}, by deparsing the
expressions given, for \code{deparse.level = 1} only if that gives a
sensible name (a \sQuote{symbol}, see \code{\LinkA{is.symbol}{is.symbol}}).

For \code{cbind} row names are taken from the first argument with
appropriate names: rownames for a matrix, or names for a vector of
length the number of rows of the result.

For \code{rbind} column names are taken from the first argument with
appropriate names: colnames for a matrix, or names for a vector of
length the number of columns of the result.
\end{Value}
\begin{Section}{Data frame methods}
The \code{cbind} data frame method is just a wrapper for
\code{\LinkA{data.frame}{data.frame}(..., check.names = FALSE)}.  This means that
it will split matrix columns in data frame arguments, and convert
character columns to factors unless \code{stringsAsFactors = TRUE} is
passed.

The \code{rbind} data frame method first drops all zero-column and
zero-row arguments.  (If that leaves none, it returns the first
argument with columns otherwise a zero-column zero-row data frame.)
It then takes the classes of the columns from the
first data frame, and matches columns by name (rather than by
position).  Factors have their levels expanded as necessary (in the
order of the levels of the levelsets of the factors encountered) and
the result is an ordered factor if and only if all the components were
ordered factors.  (The last point differs from S-PLUS.)  Categories
(integer vectors with levels) are promoted to factors.
\end{Section}
\begin{Section}{Dispatch}
The method dispatching is \emph{not} done via
\code{\LinkA{UseMethod}{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 class
memberships from the class attribute.

\item We inspect each class in turn to see if there is an
an applicable method.

\item If we find an applicable method we make sure that it is
identical to any method determined for prior arguments.
If it is identical, we proceed, otherwise we immediately
drop through to the default code.
}
If you want to combine other objects with data frames, it may be
necessary to coerce them to data frames first.  (Note that this
algorithm can result in calling the data frame method if all the
arguments are either data frames or vectors, and this will result in
the coercion of character vectors to factors.)
\end{Section}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{c}{c}} to combine vectors (and lists) as vectors,
\code{\LinkA{data.frame}{data.frame}} to combine vectors and matrices as a data
frame.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
m <- cbind(1, 1:7) # the '1' (= shorter vector) is recycled
m
m <- cbind(m, 8:14)[, c(1, 3, 2)] # insert a column
m
cbind(1:7, diag(3))# vector is subset -> warning

cbind(0, rbind(1, 1:3))
cbind(I=0, X=rbind(a=1, b=1:3))  # use some names
xx <- data.frame(I=rep(0,2))
cbind(xx, X=rbind(a=1, b=1:3))   # named differently

cbind(0, matrix(1, nrow=0, ncol=4))#> Warning (making sense)
dim(cbind(0, matrix(1, nrow=2, ncol=0)))#-> 2 x 1

## deparse.level
dd <- 10
rbind(1:4, c=2, "a++" = 10, dd, deparse.level=0)# middle 2 rownames
rbind(1:4, c=2, "a++" = 10, dd, deparse.level=1)# 3 rownames (default)
rbind(1:4, c=2, "a++" = 10, dd, deparse.level=2)# 4 rownames
\end{ExampleCode}
\end{Examples}

