.BG
.VE $Header: /usr3/s/current/s/.help/RCS/sweep,v 1.2 83/12/30 19:35:20 rab Exp $
.FN sweep
.TL
sweep: Sweep Out Array Summaries
.CS
sweep(a, margin, stats, fun, arg ...)
.PP
.AG a
array.  Missing values (NAs) are allowed.
.AG margin
vector describing the dimensions of `a' that correspond to
`stats'.
.AG stats
vector giving a summary statistic of array `a' that is to be
swept out.  Missing values (NAs) are allowed.
.AG fun
character string name of function to be used in sweep
operation.  Default "\-" (subtraction).
.AG arg
optional arguments to `fun'.
.RT
an array like `a', but with marginal statistics swept out as
defined by the other arguments. In the most common cases,
the function is "\-" or "/" to subtract or divide by statistics
that result from using the `apply' function on the array.
For example: `colmean<-apply(z,2,"mean")' computes
column means of array `z'.  `zcenter<-sweep(z,2,colmean)'
removes the column means.
.PP
More generally, based on `margin', there are one or more values of `a'
that would be used by `apply' to create `stats'.  `sweep' creates an
array like `a' where the corresponding value of `stats' is used in
place of each value of `a' that would have been used to create
`stats'.  The function `fun' is then used to operate
element-by-element on each value in `a' and in the constructed array.
.EX
a <- sweep(a,2,apply(a,2,"mean")) # remove col means
a <- sweep(a,1,apply(a,1,"mean")) # row means
     # a simple two-way analysis
.KW multi*
.KW apply*
.WR
