cbind
Building a Matrix from Columns or Rows

Description

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

Usage

cbind(..., deparse.level = 1)
## S3 method for class 'ts':
cbind(..., deparse.level = 1)

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

Arguments

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

For cbind.ts method, this argument can be one or more time series objects or objects can be coerced to time series. All these time series must have same frequency.

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

For cbind.ts method, this argument is not used yet, but must be default value 1.

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 have classes, the appropriate S3 or S4 method is called. This method is found as follows: First, for each argument with class XX it searches for an S3 method "cbind.XX" (or "rbind.XX" when executing rbind). If an S3 object has multiple classes, these are searched in order. If an argument is an S4 object, the class and all of its superclasses are searched. If this search produces the same S3 method for all arguments with classes, it is called. If this search finds more than one S3 method, this is a conflict, and no S3 methods are called. In this case, if any of the arguments are S4 objects, it calls methods:::cbind (or methods:::rbind), which will repeatedly call methods::cbind2 (or methods::rbind2) to process the arguments two at a time. If neither an S3 or S4 method is found, the arguments are processed as if they had no classes.
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 contains as many rows or columns as the length of the longest vector. Shorter vectors are repeated.
The dimnames attribute of the returned matrix is constructed as follows:
cbind.ts is an invisible method, it uses the same way as ts.union to combine one or more time series, and return the value as a time series(not data frames). See ts.union for more details.
Value
returns a matrix composed of adjoining columns (cbind) or rows (rbind). This matrix can have a dimnames attribute.
For cbind.ts method, returns a union time series.
Warning
When you write a function, you could build a matrix by using a statement such as x <- cbind(x, tmp.vec) inside of a for loop.
Better yet, create a matrix that is the final size before you enter the for loop: Then use replacement inside the for loop: This latter strategy uses much less memory than the method using cbind. (Use the cbind method only when you do not know the eventual size of the matrix.)
See Also
cbind.data.frame, rbind.data.frame, ts.union, c,
Examples
# add column of ones
df <- data.frame(a = c(1:5), b = (1:5)^2)
cbind(1,  df) 
# add 2 new rows
rbind(df, c(2, 3), c(5, 6))
# 3 column matrix with column dimnames
cbind(Index = c(1:3), Age = c(30, 45, 34), Salary = c(500, 600, 550)) 
# 4 combining two named vectors
mil1 <- c(c=3,d=4,e=5)
mil2 <- c(f=6,g=7,h=8)
# column names will be mil1, mil2, rownames will be c, d, e
cbind(mil1, mil2)  
# column names will be mil2, mil1, rownames will be f, g, h
cbind(mil2, mil1) 

# combine time series. # "mdeaths", "fdeaths" and "ldeaths" are data sets from R cbind(mdeaths, fdeaths, ldeaths) cbind(cbind(mdeaths, fdeaths), ldeaths) # The dim names of above two examples are different.

Package base version 6.1.1-7
Package Index