Base R functions
rbind bind columns and rows, but there's no built-in function for binding higher dimensional datastructures like matrices.
Abind takes a sequence of
vectors, matrices, or arrays and produces a single array of
the same or higher dimension.
Abind(..., along = N, rev.along = NULL, new.names = NULL, force.array = TRUE, make.names = FALSE, use.first.dimnames = FALSE, hier.names = FALSE, use.dnns = FALSE)
Any number of vectors, matrices, arrays, or data frames.
The dimensions of all the arrays must match, except on one dimension
Alternatively, there can be one (and only one) list argument supplied, whose components are the objects to be bound together. Names of the list components are treated in the same way as argument names.
The dimension along which to bind the arrays.
The default is the last dimension, i.e., the maximum length of the dim
attribute of the supplied arrays.
Alternate way to specify the dimension along which to bind the arrays:
If new.names is a list, it is the first choice for the
dimnames attribute of the result. It should have the same
structure as a dimnames attribute. If the names for a
particular dimension are
When dimension names are present on more than one
argument, should dimension names for the result be take from
the first available (the default is to take them from the
last available, which is the same behavior as
The dimensions of the supplied vectors or arrays do not need
to be identical, e.g., arguments can be a mixture of vectors
Abind coerces arguments by the addition
of one dimension in order to make them consistent with other
along=. The extra dimension is
added in the place specified by
The default action of Abind is to concatenate on the last
dimension, rather than increase the number of dimensions.
For example, the result of calling Abind with vectors is a
longer vector (see first example below). This differs from
the action of
rbind and cbind which is to return a matrix when
called with vectors. Abind can be made to behave like cbind
on vectors by specifying
along=2, and like rbind by
The dimnames of the returned object are pieced together
from the dimnames of the arguments, and the names of the
arguments. Names for each dimension are searched for in the
following order: new.names, argument name, dimnames (or
names) attribute of last argument, dimnames (or names)
attribute of second last argument, etc. (Supplying the
use.first.dimnames=TRUE changes this to
Abind to use dimnames or names from the
first argument first. The default behavior is the same as
cbind: use dimnames
from later arguments.) If some names are supplied for the
along dimension (either as argument names or dimnames in
arguments), names are constructed for anonymous arguments
An array with a dim attribute calculated as follows.
rMin=min(sapply(list(...), function(x) length(dim(x)))) and
rMax=max(sapply(list(...), function(x) length(dim(x)))) (where
the length of the dimensions of a vector are taken to be 1). Then
rMax should be
equal to or one greater than
along refers to an existing dimension, then the length of
the dim attribute of the result is
not refer to an existing dimension, then
rMax should equal
rMin and the length of the dim attribute of the result will be
called to compute the result if (a)
force.array=FALSE; and (b) the result will be a
It would be nice to make
Abind() an S3 generic, but S3 generics
cannot dispatch off anonymous arguments.
The ability of
Abind() to accept a single list argument removes
much of the need for constructs like
list.of.arrays). Instead, just do
direct construct is preferred because
do.call() construct can
sometimes consume more memory during evaluation.
Tony Plate <email@example.com> and Richard Heiberger
# Five different ways of binding together two matrices x <- matrix(1:12, 3, 4) y <- x + 100 dim(Abind(x, y, along=0)) # binds on new dimension before first dim(Abind(x, y, along=1)) # binds on first dimension dim(Abind(x, y, along=1.5)) dim(Abind(x, y, along=2)) dim(Abind(x, y, along=3)) dim(Abind(x, y, rev.along=1)) # binds on last dimension dim(Abind(x, y, rev.along=0)) # binds on new dimension after last # Unlike cbind or rbind in that the default is to bind # along the last dimension of the inputs, which for vectors # means the result is a vector (because a vector is # treated as an array with length(dim(x))==1). Abind(x=1:4, y=5:8) # Like cbind Abind(x=1:4, y=5:8, along=2) Abind(x=1:4, matrix(5:20, nrow=4), along=2) Abind(1:4, matrix(5:20, nrow=4), along=2) # Like rbind Abind(x=1:4, matrix(5:20, nrow=4), along=1) Abind(1:4, matrix(5:20, nrow=4), along=1) # Create a 3-d array out of two matrices Abind(x=matrix(1:16, nrow=4), y=matrix(17:32, nrow=4), along=3) # Use of hier.names Abind(x=cbind(a=1:3, b=4:6), y=cbind(a=7:9, b=10:12), hier.names=TRUE) # Use a list argument Abind(list(x=x, y=x), along=3) # Use lapply(..., get) to get the objects an <- c('x', 'y') names(an) <- an Abind(lapply(an, get), along=3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.