.BG
.VE $Header: /usr3/s/current/s/.help/RCS/index,v 1.2 83/12/30 19:29:25 rab Exp $
.FN index
.TL
index: Compute Position in Array
.CS
index( cat1, cat2, ... )
.PP
.AG cati
categories (as produced by `code' or `cut').
Missing values (NAs) are allowed.
.RT
structure with components `Data' and `Label':
.RC Data
vector of integers, giving the position in a contingency
table corresponding to each value of the input categories.
.RC Label
structure with one component for each input category; the
component corresponding to `cat1' is named "cat1" and is a
character vector containing the values from `cat1$Label',
etc.
.PP
The `index' function is typically used in conjunction with `tapply' in
order to handle "ragged arrays".  A ragged array is an array data
structure in which the last dimension is of variable length.  For
example, suppose that there is a measurement of strength done on a
number of individuals in different age groups.  There may be 10
observations corresponding to the first age group, 12 corresponding to
the second group, etc.  This is a 2-dimensional ragged array.
.PP
Suppose there is a further categorical variable for each individual
giving height.  A 3-way ragged array could be constructed which has
strength observations categorized by combined age/height.
.EX
ht <- cut(height,c(5,5.5,6,6.5))  # height intervals in feet
ag <- cut(age,seq(0,100,10))   # age groups
sx <- code(sex,c(1,2),c("Male","Female"))
# we have three categories categorizing strength observations
position <- index(ht,ag,sx)
mn.strength <- tapply(strength,position,"mean")
no.obs <- tapply(strength,position,"len")
tprint(strength=strength,nobs=no.obs)   # print a pretty table

fit <- mn.strength[position]
resid <- strength - fit
.PP
Notice the use of the position vector (produced by `index') to
subscript the mn.strength table in order to give fitted values.  This
can be done because even the 3-way array mn.strength can be
subscripted as if it were a vector.
.PP
The functions `index' and `tapply' can be used to perform
multidimensional summaries similar to the one-dimensional grouping
done by the `split' and `sapply' functions.  Notice that `split'
produces a data structure corresponding to a 2-dimensional ragged
array, whereas `index' does not.
.KW basic*
.KW category
.KW contingency table
.KW multi*
.WR
