Build Matrix from Columns or Rows

DESCRIPTION:

Returns a matrix that is pieced together from several vectors and/or matrices. The functions cbind and rbind are generic. In particular, there are cbind and rbind methods for data frames and bdFrames.

USAGE:

cbind(..., deparse.level=1)
rbind(..., deparse.level=1)

REQUIRED ARGUMENTS:

...
vectors and/or matrices. Missing values ( NAs) are allowed.

OPTIONAL ARGUMENTS:

deparse.level
value determining the construction of labels (column labels for cbind or row labels for rbind). This only applies to unnamed vector (non-matrix) arguments. If deparse.level is 0, the corresponding row or column has no label; if it is 2, the label is the deparsed form of the argument; and if it is 1, the deparsed form is used only if the argument is a simple name, any other expression results in no label. These are the only acceptable values for this argument.

VALUE:

matrix composed by adjoining columns ( cbind) or rows ( rbind). This matrix may have a dimnames attribute.

DETAILS:

The default methods for cbind and rbind are built into the generic functions. That is, there are no cbind.default or rbind.default functions.

If any of the input data objects are classed, the appropriate method for that class is dispatched; you are warned if incompatible methods are dispatched.

If several arguments are matrices, they must contain the same number of rows ( cbind) or columns ( rbind). In addition, all arguments that have names attributes must have the same length, which also must match the number of rows/columns of any matrix arguments.

Vector arguments are treated as row vectors by rbind and column vectors by cbind. If all arguments are vectors, the result will contain as many rows or columns as the length of the longest vector. Shorter vectors will be repeated.

The dimnames attribute of the returned matrix is constructed as follows: for any matrix argument, its dimnames attribute is carried over; for any vector argument with a names attribute, the names attribute becomes the row (column) component of dimnames for cbind ( rbind); any vector argument may be given in name=value form, where the name specifies its corresponding dimnames entry. If cbind is given more than one named vector or matrix, the rownames of the result are the names of the last named vector or matrix with rownames in the argument list (and similarly for the column names the result of rbind).

If arguments are time series, no attempt is made to relate their time parameters. See ts.union and ts.intersect for this case. Arguments that are NULL or zero-length are ignored.

WARNING:

When writing a function, you could build a matrix by using a statement such as x <- cbind(x, tmp.vec) inside of a for loop.

A better way to do the same thing is to create a matrix which is the final size before entering the for loop: x <- matrix(nrow=n, ncol=p) Then use replacement inside the for loop: x[,i] <- tmp.vec This latter strategy uses much less memory than the method using cbind. The "cbind" method should only be used when the eventual size of the matrix is unknown.

SEE ALSO:

, , , ,

EXAMPLES:

# add column of ones
cbind(1, xmatr) %*% regr$coef
# add 2 new rows
rbind(matrix, newrow1, newrow2)
# 3 column matrix with column dimnames
cbind(gnp=gnp, income=income, unemployment=unemployment)
# 4 combining two named vectors
mil1 <- c(c=3,d=4,e=5)
mil2 <- c(f=6,g=7,h=8)
cbind(mil1, mil2)
# columnames will be mil1, mil2
# rownames will be f g h