sweep?
On Mar 17, 2009, at 4:59 AM, Wacek Kusnierczyk wrote:
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*:
The sweep operation is really being done by first lining up the second argument, statistic vector, with either the rows or columns of the first argument matrix in the same sense as with apply. The sweeping is then done in the remaining direction(s). The confusion arises because there are really two (or more) directions of the operation and you are focussing on the second. I have no argument with your assertion that the documentation was not clear in this regard or in the meaning of "summary statistic".
David Winsemius > > > 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