R arrays
last modified November 19, 2026
An array is a vector with a dim attribute that gives it one or
more dimensions. A matrix is the special case with two dimensions, and an
array with three dimensions can be imagined as a stack of matrices, called
layers. Arrays are used for data collected under several conditions at
once, such as measurements by day, by time of day, and by week. This article
shows how to create arrays, name their dimensions, select elements with
several subscripts, and summarise them by margin.
Creating an array
The function array takes a vector of values and a dim
argument with one length per dimension. The values fill the array in
column-major order, the same order that matrix uses: down the
first column of the first layer, then the next column, and only then the next
layer.
a <- array(1:12, dim = c(2, 3, 2))
print(a)
cat("dim:", dim(a), "\n")
The printout shows two layers, each a two-by-three matrix. The first layer
contains the values 1 to 6 and the second contains 7 to 12. The function
dim returns the three lengths in order: rows, columns, and layers.
$ Rscript building.R
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
dim: 2 3 2
Dimension names
dimnames is a list with one character vector per dimension. The
first vector names the rows, the second the columns, and the third the layers.
The names travel with the array through indexing and printing, so a result can
be labelled without extra work.
a <- array(1:8, dim = c(2, 2, 2),
dimnames = list(c("r1", "r2"), c("c1", "c2"), c("t1", "t2")))
print(a)
The array has two rows, two columns, and two layers, and every position has a label. The printout uses the layer names as headings above each block. As with a matrix, the names are attributes rather than data, so they do not change the type of the elements.
$ Rscript dimension-names.R , , t1 c1 c2 r1 1 3 r2 2 4 , , t2 c1 c2 r1 5 7 r2 6 8
Indexing with several subscripts
Each dimension gets its own subscript inside one pair of brackets. A blank
subscript means every position along that dimension, and the trailing comma is
required when the last dimension is left open. The result drops dimensions of
length one unless drop = FALSE is given.
a <- array(1:12, dim = c(2, 3, 2))
cat("a[1, 2, 1]:", a[1, 2, 1], "\n")
cat("row 1 of layer 1:", a[1, , 1], "\n")
cat("element 2,3 in both layers:", a[2, 3, ], "\n")
The element at row one, column two, layer one is 3. The whole first row of the first layer is the vector 1, 3, 5. Leaving the layer subscript blank returns the values from both layers, 6 and 12. The same rules scale to four or more dimensions, though such arrays become hard to read and are rare in practice.
$ Rscript indexing.R a[1, 2, 1]: 3 row 1 of layer 1: 1 3 5 element 2,3 in both layers: 6 12
Summaries by margin
apply works on arrays as it does on matrices, with one margin
number per dimension. Margin 1 applies the function to each row, margin 2 to
each column, and margin 3 to each layer. The dimension that is not named in the
margin is the one that varies inside each call.
a <- array(1:12, dim = c(2, 3, 2))
cat("sum over rows:", apply(a, 1, sum), "\n")
cat("sum over columns:", apply(a, 2, sum), "\n")
cat("sum over layers:", apply(a, 3, sum), "\n")
The row sums are 36 and 42, the column sums are 18, 26, and 34, and the layer
sums are 21 and 57. Every set of sums adds to 78, the total of the numbers one
to twelve, which is a useful check that the margins are correct. A margin can
also be a vector such as c(1, 2) to summarise whole layers.
$ Rscript margins.R sum over rows: 36 42 sum over columns: 18 26 34 sum over layers: 21 57
A complete example
The next script records temperatures for three days, at two times of day, over
two weeks. The three dimensions are day, time, and week, and
dimnames supplies a label for each position. The daily mean
averages over time and week, and the weekly mean averages over day and time.
temps <- array(c(18, 20, 22, 19, 21, 23, 15, 16, 17, 14, 15, 16),
dim = c(3, 2, 2),
dimnames = list(c("Mon", "Tue", "Wed"),
c("morning", "evening"),
c("week1", "week2")))
print(temps)
cat("daily mean:", apply(temps, 1, mean), "\n")
cat("week mean:", apply(temps, 3, mean), "\n")
The first week is warmer than the second. The evening readings are one degree warmer than the morning readings in the first week and one degree cooler in the second. The daily means are 16.5, 18, and 19.5, and the weekly means are 20.5 and 15.5. Keeping the three dimensions separate makes it possible to ask either question without reshaping the data.
$ Rscript temperatures.R
, , week1
morning evening
Mon 18 19
Tue 20 21
Wed 22 23
, , week2
morning evening
Mon 15 14
Tue 16 15
Wed 17 16
daily mean: 16.5 18 19.5
week mean: 20.5 15.5
Next steps
The factors article turns text categories into a form that statistical functions understand. The apply-family article later in the series covers the whole family of iteration functions, and the data frames article shows the two-dimensional structure that most real data uses.
Source
An Introduction to R: arrays and matrices
The R Language Definition: indexing
In this article we have created arrays with the dim argument,
named their rows, columns, and layers, and selected elements with several
subscripts. We summarised rows, columns, and layers with apply,
and worked through a temperature example with three dimensions. The series
continues with R factors.
Author
List all R Language tutorials.