sweep() function in R Language is used to apply the operation “+ or -” to the row or column in data matrix.
It is used to sweep the values from the data-framework.
Syntax:
sweep(x, MARGIN, STATS, FUN)Parameters:
x: Typically a matrix.
MARGIN: MARGIN = 1 means row; MARGIN = 2 means column.
STATS: the value that should be added or subtracted
FUN: The operation that has to be done (e.g. + or -)
Example 1: Sweep Matrix
# R program to illustrate # sweep matrix # Create example matrix data <- matrix(0, nrow = 6, ncol = 4) # Apply sweep in R data_ex1 <- sweep(x = data, MARGIN = 1, STATS = 5, FUN = "+") # Print example 1 print(data_ex1) |
Output:
[,1] [,2] [,3] [,4]
[1,] 5 5 5 5
[2,] 5 5 5 5
[3,] 5 5 5 5
[4,] 5 5 5 5
[5,] 5 5 5 5
[6,] 5 5 5 5
Here in the above code, the value of the matrix was 0, which was then sweeped by the sweep() function and the new value of the matrix became 5.
Example 2: Using sweep() with stats
# R program to illustrate # sweep funtion with stats # Create example matrix data <- matrix(0, nrow = 6, ncol = 4) # Sweep with Complex STATS data_ex2 <- sweep(x = data, MARGIN = 1, STATS = c(1, 2, 3, 4, 5, 6), FUN = "+") # Print example 2 print(data_ex2) |
Output:
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 2 2 2 2
[3,] 3 3 3 3
[4,] 4 4 4 4
[5,] 5 5 5 5
[6,] 6 6 6 6
Here in the above example, we have used the sweep() function along with the stats.
Recommended Posts:
- Rounding off values in R Language - round() Function
- Condense Column Values of a Data Frame in R Programming - summarise() Function
- Modify values of a Data Frame in R Language - transform() Function
- Calculate the Mean of each Column of a Matrix or Array in R Programming - colMeans() Function
- Combine Vectors, Matrix or Data Frames by Columns in R Language - cbind() Function
- Combine Vectors, Matrix or Data Frames by Rows in R Language - rbind() Function
- Return a Matrix with Upper Triangle as TRUE values in R Programming - upper.tri() Function
- Return a Matrix with Lower Triangle as TRUE values in R Programming - lower.tri() Function
- Check if the Object is a Matrix in R Programming - is.matrix() Function
- Convert an Object into a Matrix in R Programming - as.matrix() Function
- Converting a List to Vector in R Language - unlist() Function
- Convert elements of a Vector to Strings in R Language - toString() Function
- Calculate Square root of a number in R Language - sqrt() Function
- Getting Multiplication of the Objects passed as Arguments in R Language - prod() Function
- Remove Leading whitespaces from a String in R Language - trimws() Function
- Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
- Case conversion of a String in R Language - toupper(), tolower(), casefold() and cartr() Function
- Get the natural logarithm of the beta value of Non-Negative numeric vectors in R Language - lbeta() Function
- Difference Between C Language and LISP Language
- Generate a Vector of specified length with each element as a unique color on RGB scale in R Language - topo.colors() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

