Home » How to Use the sweep Function in R (With Examples)

How to Use the sweep Function in R (With Examples)

by Erma Khan

You can use the sweep() function in R to perform some operation on either the rows or columns of a matrix.

This function uses the following basic syntax:

sweep(x, MARGIN, STATS, FUN)

where:

  • x: Name of the matrix
  • MARGIN: The margin to perform function on (1=rows, 2=columns)
  • STATS: The value(s) to use in the function
  • FUN: The function to perform

The following examples show how to use the sweep() function in different scenarios in R.

Example 1: Use sweep() to Perform Operation on Rows

The following code shows how to use the sweep() function to add a specific number to the values in each row of the matrix:

#define matrix
mat 5)

#view matrix
mat

     [,1] [,2] [,3]
[1,]    1    6   11
[2,]    2    7   12
[3,]    3    8   13
[4,]    4    9   14
[5,]    5   10   15

#add specific numbers to each row
sweep(mat, 1, c(5, 10, 15, 20, 25), "+")

     [,1] [,2] [,3]
[1,]    6   11   16
[2,]   12   17   22
[3,]   18   23   28
[4,]   24   29   34
[5,]   30   35   40

Here’s how the sweep() function worked in this scenario:

  • 5 was added to each value in the first row.
  • 10 was added to each value in the second row.
  • 15 was added to each value in the third row.
  • 20 was added to each value in the fourth row.
  • 25 was added to each value in the fifth row.

Note that in this example we used addition (+) as the mathematical operation to perform, but we could choose to use a different operation.

For example, the following code shows how to multiply the values in each row by certain numbers:

#define matrix
mat 5)

#view matrix
mat

     [,1] [,2] [,3]
[1,]    1    6   11
[2,]    2    7   12
[3,]    3    8   13
[4,]    4    9   14
[5,]    5   10   15

#multiply values in each row by certain amount
sweep(mat, 1, c(.5, 1, 2, 4, 6), "*")

     [,1] [,2] [,3]
[1,]  0.5    3  5.5
[2,]  2.0    7 12.0
[3,]  6.0   16 26.0
[4,] 16.0   36 56.0
[5,] 30.0   60 90.0

Example 2: Use sweep() to Perform Operation on Columns

The following code shows how to use the sweep() function to add a specific number to the values in each column of the matrix:

#define matrix
mat 5)

#view matrix
mat

     [,1] [,2] [,3]
[1,]    1    6   11
[2,]    2    7   12
[3,]    3    8   13
[4,]    4    9   14
[5,]    5   10   15

#add specific numbers to each column
sweep(mat, 2, c(5, 10, 15), "+")

     [,1] [,2] [,3]
[1,]    6   16   26
[2,]    7   17   27
[3,]    8   18   28
[4,]    9   19   29
[5,]   10   20   30

Here’s how the sweep() function worked in this scenario:

  • 5 was added to each value in the first column.
  • 10 was added to each value in the second column.
  • 15 was added to each value in the third column.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Create an Empty Matrix in R
How to Convert Data Frame to Matrix in R
How to Plot the Rows of a Matrix in R

Related Posts