.BG
.VE $Header: /usr3/s/current/s/.help/RCS/tapply,v 1.2 83/12/30 19:35:36 rab Exp $
.FN tapply
.TL
tapply: Apply a Function to a Ragged Array
.CS
tapply(x, indices, fun, arg1, ...)
.PP
.AG x
vector of data to be grouped by `indices'.
Missing values (NAs) are allowed if `fun' accepts them.
.AG indices
data structure containing a vector named `Data' and a
labelling structure named `Label'.  `Data' is a vector of
the same length as `x' describing the position in a multi-way
array corresponding to each `x' observation.  `Label' is
a label structure that contains one character vector
component for each dimension of the multi-way array.  This
argument is generally the output from the `index' function.
Missing values (NAs) are allowed.
.AG fun
character string giving the name of the function to be
applied to each group.
.AG arg1
optional arguments to be given to each invocation of `fun'.
.RT
if `fun' returns a single value for each group (e.g. "mean",
"var", etc.) then `tapply' returns a multi-way table
containing a multi-way array and a component named `Label'.
If `fun' returns a vector of the same length as each group
(e.g. "sort"), then `tapply' returns a vector result like
`x'.  `Tapply' generates an error if the `fun' returns a
vector of any other length (although it should eventually
handle any constant-length result, e.g. "range".)
.EX
# generate mean republican votes for regions of the U.S.
# category that gives the region for each observation
region <- state.region[row(votes.repub)]
election <- code(votes.year)[col(votes.repub)]
pos <- index(region,election)
mn <- tapply(votes.repub,pos,"mean")
tprint(round(mn,1))  # table of mean vote by region and election
resid <- votes.repub - mn[pos]
.KW basic*
.KW apply*
.KW category
.KW contingency table
.KW multi*
.WR
