Scale the Columns of a Matrix in R Programming – scale() Function

scale() function in R Langauge is a generic function which centers and scales the columns of a numeric matrix. The 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 of x
scale: represents either logical value or numeric alike vector equal to the number of x

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


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



My Personal Notes arrow_drop_up

Recommended Posts:


Blockchain Enthusiast

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.