Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (7.3k points)
edited by

When I look at the source of R Packages, I see the function sweep used quite often. Sometimes it's used when a simpler function would have sufficed (e.g., apply), other times, it's impossible to know exactly what it's is doing without spending a fair amount of time to step through the code block it's in.

The fact that I can reproduce sweep's effect using a simpler function suggests that i don't understand sweep's core use cases, and the fact that this function is used so often suggests that it's quite useful.

The context:

sweep is a function in R's standard library; its arguments are:

sweep(x, MARGIN, STATS, FUN="-", check.margin=T, ...)

# x is the data

# STATS refers to the summary statistics which you wish to 'sweep out'

# FUN is the function used to carry out the sweep, "-" is the default

As you can see, the arguments are similar to apply though sweep requires one more parameter, STATS.

Another key difference is that sweep returns an array of the same shape as the input array, whereas the result returned by apply depends on the function passed in.

sweep in action:

# e.g., use 'sweep' to express a given matrix in terms of distance from 

# the respective column mean

# create some data:

M = matrix( 1:12, ncol=3)

# calculate column-wise mean for M

dx = colMeans(M)

# now 'sweep' that summary statistic from M

sweep(M, 2, dx, FUN="-")

     [,1] [,2] [,3]

[1,] -1.5 -1.5 -1.5

[2,] -0.5 -0.5 -0.5

[3,]  0.5  0.5  0.5

[4,]  1.5  1.5  1.5

So in sum, what I'm looking for is an exemplary use case or two for sweep.

Please, do not recite or link to the R Documentation, mailing lists, or any of the 'primary' R sources--assume I've read them. What I'm interested in is how experienced R programmers/analysts use sweep in their own code.

1 Answer

0 votes
edited by

You can use the sweep function to operate on a matrix by row or column.



defines whether the operation is performed row-wise or column-wise.


Defines the values used for the operation.


Defines the operation to be performed.

For example:

 To add 1 to the 1st row, 2 to the 2nd, and so on, to the following matrix:

 M = matrix( 1:12, ncol=3)

> M

     [,1] [,2] [,3]

[1,]    1    5    9

[2,]    2    6   10

[3,]    3    7   11

[4,]    4    8   12

 sweep (M, 1, c (1: 4), "+")

     [,1] [,2] [,3]

[1,]    2    6   10

[2,]    4    8   12

[3,]    6   10   14

[4,]    8   12   16

Browse Categories