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.
sweep(x, MARGIN, STATS, FUN)
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
[,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
[,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.
- 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
- Remove Leading whitespaces from a String in R Language - trimws() Function
- Getting Multiplication of the Objects passed as Arguments in R Language - prod() Function
- Convert elements of a Vector to Strings in R Language - toString() Function
- Calculate Square root of a number in R Language - sqrt() Function
- Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
- Get the natural logarithm of the beta value of Non-Negative numeric vectors in R Language - lbeta() Function
- Case conversion of a String in R Language - toupper(), tolower(), casefold() and cartr() Function
- Difference Between C Language and LISP Language
- Reverse the values of an Object in R Programming - rev() 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 firstname.lastname@example.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.