Data Normalization Techniques in R

Topic Overview and Actualization

Greetings, Space Voyager! Today, we're venturing into the concept of Data Normalization. This technique aims to render numerical data comparable by scaling it down. In this lesson, you will familiarize yourself with the data normalization process and discover how to apply it using R.

Understanding Data Normalization

Data normalization is a process that transforms your data, allowing for unbiased and sensible comparisons. If datasets comprise varying scales or units, it's possible that certain data elements could unfairly skew the analysis. By amending these differences, data normalization ensures equality among all data, irrespective of their initial scale or unit. This assurance prevents favoritism toward specific data due to their scale or units and supports accuracy and equitability in data analysis.

Common Data Normalization Techniques

We'll walk you through two mainstream normalization techniques: Min-Max and Z-Score:

  1. Min-Max Normalization: This technique rescales a variable to range between 0 and 1. The mathematical expression is: xnew=x−xminxmax−xmin x_{new} = \frac{x - x_{min}}{x_{max} - x_{min}}

    Post-transformation, the new lowest and highest values of the dataset will be 0 and 1, respectively. This linear transformation doesn't change the shape of the distribution, just the scale.

  2. Z-Score Normalization: This technique enables data to have a mean of 0 and a standard deviation of 1. Its formula is: z=x−μσ z = \frac{x - μ}{σ}

    Here, μ represents the mean value, while σ stands for the standard deviation.

    This scaling method isn't subjected to the min-max limitation. It's practical when the data aren't uniformly distributed. Following standardization, the distribution will exhibit a standard deviation of 1, mean of 0, and all outliers will stand out.

Data Normalization Using R

Now, let's put theory into practice. Consider the Height dataset of some Space Explorers:

R
df <- data.frame(
    "Space Explorer" = c('Spock', 'Kirk', 'McCoy', 'Scotty'),
    "Height" = c(183, 178, 170, 178)
)

To normalize using Min-Max in R, here's the corresponding code, implementing the described formula:

R
df$Height <- (df$Height - min(df$Height)) / (max(df$Height) - min(df$Height))
print(df$Height)
# After normalization, df$Height is [1.00 0.615 0.00 0.615]

For Z-Score there is an implemented function, called scale:

R
df$Height <- scale(df$Height)
print(df$Height)
# After normalization, df$Height is [1.07 0.139 -1.35 0.139]
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal