Index arrays



next up previous contents
Next: The array() function Up: Arrays and matrices Previous: Array indexing. Subsections

Index arrays

  As well as an index vector in any subscript position, an array may be used with a single index array in order either to assign a vector of quantities to an irregular collection of elements in the array, or to extract an irregular collection as a vector.

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:

In this case we need a subscript array, as in the example given in Figure 1

 
Figure 1:   Using an index array

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)



Erik Moledor
Tue Jan 31 21:02:18 EST 1995