How to Unscale Values in R (With Example)


The scale() function in R can be used to scale the values in a vector, matrix, or data frame.

This function uses the following formula to calculate scaled values:

xscaled = (xoriginal – x̄) / s

where:

  • xoriginal: The original x-value
  • : The sample mean
  • s: The sample standard deviation

This is also known as standardizing data, which simply converts each original value into a z-score.

This formula is used in a variety of machine learning algorithms and statistics formulas when we would like each column in a data frame to have a standardized range.

However, sometimes you may want to unscale data values that have been scaled.

This is the process of retrieving the original values in a vector or data frame before we applied any scaling transformation to them.

To do so, you can use the following formula:

scaled_data * attr(scaled_data, 'scaled:scale') + attr(scaled_data, 'scaled:center')

This particular example unscales the vector of values that has been scaled and saved as scaled_data.

By using this formula, you are able to get back the original vector of values before they had the scale() function applied to them.

This formula works because the scale() function produces the following attributes:

  • scaled:scale – The sample standard deviation of the original data
  • scaled:center – The sample mean of the original data

This formula multiplies the scaled values by the sample standard deviation and then adds back the sample mean.

This allows us to retrieve the original values that we started with before we applied the scale() function.

The following examples show how to use this formula in practice.

Example: How to Unscale Values in R

Suppose we have the following vector of values in R named original_data:

#define vector of values
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Suppose that we would like to scale each value in the vector.

We can use the scale() function to do so:

#calculate scaled values in vector
scaled_data <- scale(original_data)

#view scaled values
scaled_data

            [,1]
 [1,] -1.4863011
 [2,] -1.1560120
 [3,] -0.8257228
 [4,] -0.4954337
 [5,] -0.1651446
 [6,]  0.1651446
 [7,]  0.4954337
 [8,]  0.8257228
 [9,]  1.1560120
[10,]  1.4863011
attr(,"scaled:center")
[1] 5.5
attr(,"scaled:scale")
[1] 3.02765

The output displays each of the scaled values along with the center (the mean) and the scale (the sample standard deviation) values that were used for scaling.

Here is how each scaled value was calculated:

  • Value 1: (1 – 5.5) / 3.02765 = -1.486
  • Value 2: (2 – 5.5) / 3.02765 = -1.156
  • Value 3: (3 – 5.5) / 3.02765 = -0.826

And so on.

Now suppose that we would like to unscale the values so that we can get back the original values of the vector.

We can use the following syntax to do so:

#unscale each of the scaled values in the scaled_data vector
scaled_data * attr(scaled_data, 'scaled:scale') + attr(scaled_data, 'scaled:center')

     [,1]
 [1,]    1
 [2,]    2
 [3,]    3
 [4,]    4
 [5,]    5
 [6,]    6
 [7,]    7
 [8,]    8
 [9,]    9
[10,]   10
attr(,"scaled:center")
[1] 5.5
attr(,"scaled:scale")
[1] 3.02765

The output displays each of the unscaled values along with the center (the mean) and the scale (the sample standard deviation) values that were used for unscaling.

Notice that these values match the values from the original vector, which tells us that we were able to successfully unscale all of the values.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Scale Values Between 0 and 1 in R
How to Normalize Data in R
How to Standardize Data in R
How to Average Across Columns in R

Leave a Reply

Your email address will not be published. Required fields are marked *