[R] sweep?
Wacek Kusnierczyk
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Mar 17 09:59:07 CET 2009
rkevinburton at charter.net wrote:
> I am having a hard time understanding just what 'sweep' does. The documentation states:
>
> Return an array obtained from an input array by sweeping out a summary statistic.
>
> So what does it mean "weeping out a summary statistic"?
>
from both the text and the examples in that help page, it seems that
both 'sweep' and 'summary statistics' are misleading. the argument
STATS is just about any value, vector of values, array of values, etc.,
you might like, and these values are combined, using whatever function
passed as the argument FUN, with the values in the input data. by
default the combinator function FUN is '-', hence 'sweep'.
in this example (from ?sweep, simplified), you're sweeping arbitrary
values ('summary statistics'):
A <- array(1:16, dim = c(4,4))
# sweep 1:2, with recycling
sweep(A, 1, 1:2)
in this example, you're multiplying ('sweeping') the data by some
arbitrary values ('summary statistics'):
A <- array(1:16, dim = c(4, 4))
# sweep by * 1:4, with recycling
sweep(A, 1, 1:4, '*')
be careful to note that here '1' means that the operation is performed
*columnwise*, unlike in the case of apply, where '1' means *rowwise*:
sweep(A, 1, 1:4, '*')
apply(A, 1, '*', 1:4)
(to make sense of the output, not that apply has implicitly transposed
the matrix).
be careful to note that the documentation is *wrong* wrt. the type of
input and output:
"
Arguments:
x: an array.
Value:
An array with the same shape as 'x', but with the summary
statistics swept out.
"
d = data.frame(x=rnorm(10), y = rnorm(10))
is.array(d)
# FALSE
d = sweep(d, 1, 0)
is.array(d)
# FALSE
no error reported, however.
vQ
More information about the R-help
mailing list