function in R Langauge is a generic function which centers and scales the columns of a numeric matrix. The **scale()**`center`

parameter takes either numeric alike vector or logical value. If the numeric vector is provided, then each column of the matrix has the corresponding value from `center`

subtracted from it. If the logical value is provided TRUE, then column means of the matrix is subtracted from their corresponding columns. The `scale`

takes either numeric alike vector or logical value. When provided with a numeric like vector, then each column of the matrix is divided by the corresponding value from `scale`

. If the logical value is provided in `scale`

parameter, then centered columns of the matrix is divided by their standard deviations, and the root mean square otherwise. If FALSE, no scaling is done on the matrix.

Syntax:

scale(x, center = TRUE, scale = TRUE)

Parameters:

x:represents numeric matrix

center:represents either logical value or numeric alike vector equal to the number ofx

scale:represents either logical value or numeric alike vector equal to the number ofx

**Example 1:**

`# Create matrix ` `mt <- ` `matrix` `(1:10, ncol = 5) ` ` ` `# Print matrix ` `cat` `(` `"Matrix:\n"` `) ` `print` `(mt) ` ` ` `# Scale matrix with default arguments ` `cat` `(` `"\nAfter scaling:\n"` `) ` `scale` `(mt) ` |

*chevron_right*

*filter_none*

**Output:**

Matrix: [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 1 3 5 7 9 [2, ] 2 4 6 8 10 After scaling: [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] -0.7071068 -0.7071068 -0.7071068 -0.7071068 -0.7071068 [2, ] 0.7071068 0.7071068 0.7071068 0.7071068 0.7071068 attr(, "scaled:center") [1] 1.5 3.5 5.5 7.5 9.5 attr(, "scaled:scale") [1] 0.7071068 0.7071068 0.7071068 0.7071068 0.7071068

**Example 2:**

`# Create matrix ` `mt <- ` `matrix` `(1:10, ncol = 2) ` ` ` `# Print matrix ` `cat` `(` `"Matrix:\n"` `) ` `print` `(mt) ` ` ` `# Scale center by vector of values ` `cat` `(` `"\nScale center by vector of values:\n"` `) ` `scale` `(mt, center = ` `c` `(1, 2), scale = ` `FALSE` `) ` ` ` `# Scale by vector of values ` `cat` `(` `"\nScale by vector of values:\n"` `) ` `scale` `(mt, center = ` `FALSE` `, scale = ` `c` `(1, 2)) ` |

*chevron_right*

*filter_none*

**Output:**

Matrix: [, 1] [, 2] [1, ] 1 6 [2, ] 2 7 [3, ] 3 8 [4, ] 4 9 [5, ] 5 10 Scale center by vector of values: [, 1] [, 2] [1, ] 0 4 [2, ] 1 5 [3, ] 2 6 [4, ] 3 7 [5, ] 4 8 attr(, "scaled:center") [1] 1 2 Scale by vector of values: [, 1] [, 2] [1, ] 1 3.0 [2, ] 2 3.5 [3, ] 3 4.0 [4, ] 4 4.5 [5, ] 5 5.0 attr(, "scaled:scale") [1] 1 2

## Recommended Posts:

- Calculate the Sum of Matrix or Array columns in R Programming - colSums() Function
- Getting a Matrix of number of columns in R Programming - col() Function
- Naming Rows and Columns of a Matrix in R Programming - rownames() and colnames() Function
- Get the number of columns of an Object in R Programming - ncol() Function
- Combine Vectors, Matrix or Data Frames by Columns in R Language - cbind() Function
- Check if the Object is a Matrix in R Programming - is.matrix() Function
- Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
- Convert an Object into a Matrix in R Programming - as.matrix() Function
- Transform the Scaled Matrix to its Original Form in R Programming - Using Matrix Computations
- Getting the Modulus of the Determinant of a Matrix in R Programming - determinant() Function
- Return a Matrix with Lower Triangle as TRUE values in R Programming - lower.tri() Function
- Compute Choleski factorization of a Matrix in R Programming - chol() Function
- Get or Set Dimensions of a Matrix in R Programming - dim() Function
- Calculate the Mean of each Column of a Matrix or Array in R Programming - colMeans() Function
- Calculate the cross-product of a Matrix in R Programming - crossprod() Function
- Calculate the cross-product of the Transpose of a Matrix in R Programming - tcrossprod() Function
- Compute the Sum of Rows of a Matrix or Array in R Programming - rowSums Function
- Getting the Determinant of the Matrix in R Programming - det() Function
- Construct a Diagonal Matrix in R Programming - diag() Function
- Perform Operations over Margins of an Array or Matrix in R Programming - apply() 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.