Rev 69968 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/cbind.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2016 R Core Team% Distributed under GPL 2 or later\name{cbind}\alias{cbind}%%--do C-internal method dispatching !!\alias{rbind}\alias{cbind.data.frame}\alias{rbind.data.frame}\alias{.__H__.cbind}\alias{.__H__.rbind}\concept{insert}\title{Combine R Objects by Rows or Columns}\description{Take a sequence of vector, matrix or data-frame 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)\method{rbind}{data.frame}(\dots, deparse.level = 1, make.row.names = TRUE,stringsAsFactors = default.stringsAsFactors())}\arguments{\item{\dots}{(generalized) vectors or matrices. These can be given as namedarguments. Other \R objects may be coerced as appropriate, or S4methods may be used: see sections \sQuote{Details} and\sQuote{Value}. (For the \code{"data.frame"} method of \code{cbind}these can be further arguments to \code{\link{data.frame}} such as\code{stringsAsFactors}.)}\item{deparse.level}{integer controlling the construction of labels inthe case of non-matrix-like arguments (for the default method):\cr\code{deparse.level = 0} constructs no labels; the default,\cr\code{deparse.level = 1 or 2} constructs labels from the argumentnames, see the \sQuote{Value} section below.}\item{make.row.names}{(only for data frame method:) logicalindicating if unique and valid \code{\link{row.names}} should beconstructed from the arguments.}\item{stringsAsFactors}{logical, passed to \code{\link{as.data.frame}};only has an effect when the \code{\dots} arguments contain a(non-\code{data.frame}) \code{\link{character}}.}}\details{The functions \code{cbind} and \code{rbind} are S3 generic, withmethods for data frames. The data frame method will be used if atleast one argument is a data frame and the rest are vectors ormatrices. There can be other methods; in particular, there is one fortime series objects. See the section on \sQuote{Dispatch} for howthe method to be used is selected. If some of the arguments are of anS4 class, i.e., \code{\link{isS4}(.)} is true, S4 methods are soughtalso, and the hidden \code{cbind} / \code{rbind} functionsfrom package \pkg{methods} maybe called, which in turn build on\code{\link{cbind2}} or \code{\link{rbind2}}, respectively. In thatcase, \code{deparse.level} is obeyed, similarly to the default method.In the default method, all the vectors/matrices must be atomic (see\code{\link{vector}}) or lists. Expressions are not allowed.Language objects (such as formulae and calls) and pairlists will becoerced to lists: other objects (such as names and external pointers)will be included as elements in a list result. Any classes the inputsmight have are discarded (in particular, factors are replaced by theirinternal codes).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.)Matrices are restricted to less than \eqn{2^{31}}{2^31} rows andcolumns even on 64-bit systems. So input vectors have the same lengthrestriction: as from \R 3.2.0 input matrices with more elements (butmeeting the row and column restrictions) are allowed.}\section{Data frame methods}{The \code{cbind} data frame method is just a wrapper for\code{\link{data.frame}(..., check.names = FALSE)}. This means thatit will split matrix columns in data frame arguments, and convertcharacter columns to factors unless \code{stringsAsFactors = FALSE} isspecified.The \code{rbind} data frame method first drops all zero-column andzero-row arguments. (If that leaves none, it returns the firstargument with columns otherwise a zero-column zero-row data frame.)It then takes the classes of the columns from thefirst data frame, and matches columns by name (rather than byposition). Factors have their levels expanded as necessary (in theorder of the levels of the levelsets of the factors encountered) andthe result is an ordered factor if and only if all the components wereordered factors. (The last point differs from S-PLUS.) Old-stylecategories (integer vectors with levels) are promoted to factors.}\value{For the default method, a matrix combining the \code{\dots} argumentscolumn-wise or row-wise. (Exception: if there are no inputs or allthe inputs are \code{NULL}, the value is \code{NULL}.)The type of a matrix result determined from the highest type of any ofthe inputs in the hierarchy raw < logical < integer < double < complex <character < list .For \code{cbind} (\code{rbind}) the column (row) names are taken fromthe \code{colnames} (\code{rownames}) of the arguments if these arematrix-like. Otherwise from the names of the arguments or where thoseare not supplied and \code{deparse.level > 0}, by deparsing theexpressions given, for \code{deparse.level = 1} only if that gives asensible name (a \sQuote{symbol}, see \code{\link{is.symbol}}).For \code{cbind} row names are taken from the first argument withappropriate names: rownames for a matrix, or names for a vector oflength the number of rows of the result.For \code{rbind} column names are taken from the first argument withappropriate names: colnames for a matrix, or names for a vector oflength the number of columns of the result.}\section{Dispatch}{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 anapplicable 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 all thearguments are 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{m <- cbind(1, 1:7) # the '1' (= shorter vector) is recycledmm <- cbind(m, 8:14)[, c(1, 3, 2)] # insert a columnmcbind(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## deparse.leveldd <- 10rbind(1:4, c = 2, "a++" = 10, dd, deparse.level = 0) # middle 2 rownamesrbind(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## cheap row names:b0 <- gl(3,4, labels=letters[1:3])bf <- setNames(b0, paste0("o", seq_along(b0)))df <- data.frame(a = 1, B = b0, f = gl(4,3))df. <- data.frame(a = 1, B = bf, f = gl(4,3))new <- data.frame(a = 8, B ="B", f = "1")(df1 <- rbind(df , new))(df.1 <- rbind(df., new))stopifnot(identical(df1, rbind(df, new, make.row.names=FALSE)),identical(df1, rbind(df., new, make.row.names=FALSE)))\dontshow{## Testing a semi-official use:d2 <- rbind.data.frame(as.list(df), as.list(new))d3 <- rbind.data.frame(as.list(df), as.list(new), make.row.names=FALSE)stopifnot(identical(.row_names_info(d3), -13L))## no longer: attr(d2, "row.names")[c(1,13)] == c("13", "131"))row.names(d2) <- attr(d3, "row.names")# = 1:13stopifnot(identical(d2, d3))}}\keyword{array}\keyword{manip}