Transform the Scaled Matrix to its Original Form in R Programming - Using Matrix Computations - GeeksforGeeks

18 Jan, 2022
In R programming, a matrix can be scaled and centered using scale() function. But, there is no in-built function to transform the scaled matrix back to the original matrix. In this article, we’ll learn to transform the scaled matrix back to the original matrix using some simple computations.
Attributes of the scaled matrix can be used to unscale the matrix back to its original matrix.
R
# Create matrixmt <- matrix(c(1:20), ncol = 5)  # Print original matrixcat("Original matrix:\n")print(mt)  # Scale the matrixscaled.mt <- scale(mt)  # Print Scaled matrixcat("Scaled matrix:\n")print(scaled.mt)  # Unscale the matrixunscaled.mt <- t(apply(scaled.mt, 1,                 function(r) r * attr(scaled.mt, 'scaled:scale') +                                 attr(scaled.mt, 'scaled:center')))  # Print Unscaled matrixcat("Unscaled matrix:\n")print(unscaled.mt)
Output:
Original matrix:
     [, 1] [, 2] [, 3] [, 4] [, 5]
[1, ]    1    5    9   13   17
[2, ]    2    6   10   14   18
[3, ]    3    7   11   15   19
[4, ]    4    8   12   16   20
Scaled matrix:
           [, 1]       [, 2]       [, 3]       [, 4]       [, 5]
[1, ] -1.1618950 -1.1618950 -1.1618950 -1.1618950 -1.1618950
[2, ] -0.3872983 -0.3872983 -0.3872983 -0.3872983 -0.3872983
[3, ]  0.3872983  0.3872983  0.3872983  0.3872983  0.3872983
[4, ]  1.1618950  1.1618950  1.1618950  1.1618950  1.1618950
attr(, "scaled:center")
[1]  2.5  6.5 10.5 14.5 18.5
attr(, "scaled:scale")
[1] 1.290994 1.290994 1.290994 1.290994 1.290994
Unscaled matrix:
     [, 1] [, 2] [, 3] [, 4] [, 5]
[1, ]    1    5    9   13   17
[2, ]    2    6   10   14   18
[3, ]    3    7   11   15   19
[4, ]    4    8   12   16   20

In this example, we’ll unscale the scaled matrix using mean and standard deviation of the original matrix.
R
# Create matrixa <- rnorm(5, 5, 2)b <- rnorm(5, 7, 5)   mt <- cbind(a, b)  # get center and scaling values m <- apply(df, 2, mean)s <- apply(df, 2, sd)  # Print original matrixcat("Original matrix:\n")print(mt)  # Scale the matrixscaled.mt <- scale(mt, center = m, scale = s)  # Print Scaled matrixcat("Scaled matrix:\n")print(scaled.mt)  # Unscale the matrixunscaled.mt <- t((t(scaled.mt) * s) + m)  # Print Unscaled matrixcat("Unscaled matrix:\n")print(unscaled.mt)
Original matrix:
            a          b
[1, ] 5.552301  1.9865159
[2, ] 3.936486 17.5327829
[3, ] 5.379720 15.0981877
[4, ] 3.546333  0.5230305
[5, ] 5.043194  2.0930855

Scaled matrix:
               a         b
[1, ]  0.01543556 -2.062174
[2, ] -0.69057233  5.252016
[3, ] -0.05997146  4.106591
[4, ] -0.86104456 -2.750713
[5, ] -0.20701146 -2.012036
attr(, "scaled:center")
       a        b 
5.516974 6.369655 
attr(, "scaled:scale")
       a        b 
2.288663 2.125494 

Unscaled matrix:
            a          b
[1, ] 5.552301  1.9865159
[2, ] 3.936486 17.5327829
[3, ] 5.379720 15.0981877
[4, ] 3.546333  0.5230305
[5, ] 5.043194  2.0930855
attr(, "scaled:center")
       a        b 
5.516974 6.369655 
attr(, "scaled:scale")
       a        b 
2.288663 2.125494 

gabaa406
R Matrix-Function
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to import an Excel File into R ?
How to filter R DataFrame by values in a column?
Time Series Analysis in R
R - if statement
Logistic Regression in R Programming