Update: 01/21/2014

Key words: arrayR ManualR Reference Index

array    Multi-way Arrays


Creates or tests for arrays.


array(data = NA, dim = length(data), dimnames = NULL)
as.array(x, ...)


dataa vector (including a list or expression vector) giving data to fill the array. Non-atomic classed objects are coerced by as.vector.
dimthe dim attribute for the array to be created, that is an integer vector of lengthone or more giving the maximal indices in each dimension.
dimnameseither NULL or the names for the dimensions. This must a list (or it will beignored) with one component for each dimension, either NULL or a charactervector of the length given by dim for that dimension. The list can be named, andthe list names will be used as names for the dimensions. If the list is shorter thanthe number of dimensions, it is extended by NULLs to the length required.
xan R object
....additional arguments to be passed to or from methods.


An array in R can have one, two or more dimensions. It is simply a vector which is stored with addi-tional attributes giving the dimensions (attribute "dim") and optionally names for those dimensions(attribute "dimnames").

A two-dimensional array is the same thing as a matrix.

One-dimensional arrays often look like vectors, but may be handled differently by some functions:
str does distinguish them in recent versions of R.

The "dim" attribute is an integer vector of length one or more containing non-negative values: theproduct of the values must match the length of the array.

The "dimnames" attribute is optional: if present it is a list with one component for each dimension, either NULL or a character vector of the length given by the element of the "dim" attribute for thatdimension.

is.array is a primitive function.

For a list array, the print methods prints entries of length not one in the form `integer,7' indicat-ing the type and length.


array returns an array with the extents specified in dim and naming information in dimnames. Thevalues in data are taken to be those in the array with the leftmost subscript moving fastest. If thereare too few elements in data to fill the array, then the elements in data are recycled. If data haslength zero, NA of an appropriate type is used for atomic vectors (0 for raw vectors) and NULL forlists.

Unlike matrix, array does not currently remove any attributes left by as.vector from a classed list data, so can return a list array with a class attribute.

as.array is a generic function for coercing to arrays. The default method does so by attaching a dim attribute to it. It also attaches dimnames if x has names. The sole purpose of this is to make itpossible to access the dim[names] attribute at a later returns TRUE or FALSE depending on whether its argument is an array (i.e., has a dimattribute of positive length) or not. It is generic: you can write methods to handle specific classes of objects, see InternalMethods.


is.array is a primitive function.

R 2.x.y allowed (although documented not to) a zero-length dim argument, and returned a vector oflength one.


Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

aperm, matrix, dim, dimnames.


array(1:3, c(2,4)) # recycle 1:3 "2 2/3 times"
# [,1] [,2] [,3] [,4]
#[1,] 1 3 2 1
#[2,] 2 1 3 2

R Tutorial