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.
The outer product of two arrays
An important operation on arrays is the outer product. If
a
and b
are two numeric arrays, their outer product is an
array whose dimension vector is obtained by concatenating their two
dimension vectors (order is important), and whose data vector is got by
forming all possible products of elements of the data vector of a
with those of b
. The outer product is formed by the special
operator %o%
:
> ab <- a %o% b
An alternative is
> ab <- outer(a, b, "*")
The multiplication function can be replaced by an arbitrary function of
two variables. For example if we wished to evaluate the function
f(x; y) = cos(y)/(1 + x^2)
over a regular grid of values with x- and y-coordinates
defined by the R vectors x
and y
respectively, we could
proceed as follows:
> f <- function(x, y) cos(y)/(1 + x^2) > z <- outer(x, y, f)
In particular the outer product of two ordinary vectors is a doubly subscripted array (that is a matrix, of rank at most 1). Notice that the outer product operator is of course non-commutative.
An example: Determinants of 2 by 2 single-digit matrices
As an artificial but cute example, consider the determinants of 2 by 2 matrices [a, b; c, d] where each entry is a non-negative integer in the range 0, 1, …, 9, that is a digit.
The problem is to find the determinants, ad - bc, of all possible matrices of this form and represent the frequency with which each value occurs as a high density plot. This amounts to finding the probability distribution of the determinant if each digit is chosen independently and uniformly at random.
A neat way of doing this uses the outer()
function twice:
> d <- outer(0:9, 0:9) > fr <- table(outer(d, d, "-")) > plot(fr, xlab="Determinant", ylab="Frequency")
Notice that plot()
here uses a histogram like plot method, because
it "sees" that fr
is of class "table"
.
The "obvious" way of doing this problem with for
loops, is so inefficient as
to be impractical.
It is also perhaps surprising that about 1 in 20 such matrices is singular.