[R] sweep?

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Mar 17 13:14:04 CET 2009


David Winsemius wrote:
>
>>    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.

hmm, that's ok, but i find the meaning of the MARGIN argument a little
bit confusing, esp. when the STATS argument is a vector --  as in the
example above.  you could equally well argue that in the case of apply,
the vector is aligned with rows, and the applications of FUN are done in
the other (column) direction. 

vQ




More information about the R-help mailing list