Arrays in R

An array can be considered as a multiply subscripted collection of data entries. This section provides details on the construction and manipulation of arrays.

Arrays

An array can be considered as a multiply subscripted collection of data entries, for example numeric. R allows simple facilities for creating and handling arrays, and in particular the special case of matrices.

A dimension vector is a vector of non-negative integers. If its length is k then the array is k-dimensional, e.g. a matrix is a 2-dimensional array. The dimensions are indexed from one up to the values given in the dimension vector.

A vector can be used by R as an array only if it has a dimension vector as its dim attribute. Suppose, for example, z is a vector of 1500 elements. The assignment

> dim(z) <- c(3,5,100)

gives it the dim attribute that allows it to be treated as a 3 by 5 by 100 array.

Other functions such as matrix() and array() are available for simpler and more natural looking assignments.

The values in the data vector give the values in the array in the same order as they would occur in FORTRAN, that is "column major order," with the first subscript moving fastest and the last subscript slowest.

For example if the dimension vector for an array, say a, is c(3,4,2) then there are 3 * 4 * 2 = 24 entries in a and the data vector holds them in the order a[1,1,1], a[2,1,1], …, a[2,4,2], a[3,4,2].

Arrays can be one-dimensional: such arrays are usually treated in the same way as vectors (including when printing), but the exceptions can cause confusion.


Source: R Core Team, https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Arrays-and-matrices
Creative Commons License This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 License.