Return an array obtained from an input array by sweeping out a summary statistic.
sweep(x, MARGIN, STATS, FUN = "-", check.margin = TRUE, …)
| Parameter | Description |
|---|---|
x |
an array. |
MARGIN |
a vector of indices giving the extent(s) of x which correspond to STATS. |
STATS |
the summary statistic which is to be swept out. |
FUN |
the function to be used to carry out the sweep. |
check.margin |
logical. If TRUE (the default), warn if the length or dimensions of STATS do not match the specified dimensions of x. Set to FALSE for a small speed gain when you know that dimensions match. |
… |
optional arguments to FUN. |
# NOT RUN {
require(stats) # for median
med.att <- apply(attitude, 2, median)
sweep(data.matrix(attitude), 2, med.att) # subtract the column medians
## More sweeping:
A <- array(1:24, dim = 4:2)
## no warnings in normal use
sweep(A, 1, 5)
(A.min <- apply(A, 1, min)) # == 1:4
sweep(A, 1, A.min)
sweep(A, 1:2, apply(A, 1:2, median))
## warnings when mismatch
sweep(A, 1, 1:3) # STATS does not recycle
sweep(A, 1, 6:1) # STATS is longer
## exact recycling:
sweep(A, 1, 1:2) # no warning
sweep(A, 1, as.array(1:2)) # warning
# }