A matrix example makes the process clear. In the case of a doubly indexed
array, an index matrix may be given consisting of two columns and as many
rows as desired. The entries in the index matrix are the row and column
indices for the doubly indexed array. Suppose for example we have a
array X and we wish to do the following:
.
As a less trivial example, suppose we wish to generate an (unreduced) design matrix for a block design defined by factors blocks (b levels) and varieties, (v levels). Further suppose there are n plots in the experiment. We could proceed as follows:
> Xb <- matrix(0, n, b) > Xv <- matrix(0, n, v) > ib <- cbind(1:n, blocks) > iv <- cbind(1:n, varieties) > Xb[ib] <- 1 > Xv[iv] <- 1 > X <- cbind(Xb, Xv)Further, to construct the incidence matrix, N say, we could use
N <- crossprod(Xb, Xv)
However a simpler direct way of producing this matrix is to use table():
N <- table(blocks, varieties)