[R] sweep?
David Winsemius
dwinsemius at comcast.net
Tue Mar 17 03:58:54 CET 2009
Either the rows or columns have a (possibly) varying argument vector
applied with an operator. The default operator is "-"/
> dtest <- matrix(1:25, nrow=5)
> dtest
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
> sweep(dtest, 2, 1)
[,1] [,2] [,3] [,4] [,5]
[1,] 0 5 10 15 20 # -1 argument recycling
[2,] 1 6 11 16 21
[3,] 2 7 12 17 22
[4,] 3 8 13 18 23
[5,] 4 9 14 19 24
> sweep(dtest, 2, 1:5)
[,1] [,2] [,3] [,4] [,5]
[1,] 0 4 8 12 16 # -1 -2 -3 -4 -5
[2,] 1 5 9 13 17 # |
[3,] 2 6 10 14 18 # |
[4,] 3 7 11 15 19 # \/
[5,] 4 8 12 16 20
> sweep(dtest, 1, 1:5)
[,1] [,2] [,3] [,4] [,5]
[1,] 0 5 10 15 20 # -1 --->
[2,] 0 5 10 15 20 # -2
[3,] 0 5 10 15 20 # -3
[4,] 0 5 10 15 20 # -4
[5,] 0 5 10 15 20 # -5
> sweep(dtest, 1, 1:5, FUN="+")
[,1] [,2] [,3] [,4] [,5]
[1,] 2 7 12 17 22 # +1
[2,] 4 9 14 19 24 # etc
[3,] 6 11 16 21 26
[4,] 8 13 18 23 28
[5,] 10 15 20 25 30
On Mar 16, 2009, at 10:25 PM, <rkevinburton at charter.net> <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"?
>
> Thank you.
>
> Kevin
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list