scale_minmax()
An Brief Introduction to Min-Max Scaling
In min-max scaling, values are recoded to have a particular range. The simplest approach is to recode values to have range \([0, 1]\) (see Eq. 1).
\[\begin{equation}\tag{Eq. 1: Range [0, 1]} x' = \frac{\left(x - min(x)\right)}{max(x) - min(x)} \end{equation}\]
Alternatively, values can be recoded to have range \([a, b]\) (see Eq. 2).
\[\begin{equation}\tag{Eq. 2: Range [a, b]} x' = \frac{\left(x - min(x)\right)\left(b - a\right)}{max(x) - min(x)} + a \end{equation}\]
Min-Max Scaling in R
- All code blocks on this page can be copied by clicking
in the upper right corner. - As with all content on my site, please feel free to reach out if you have any questions.
The Function
Examples
Example 1
To use the function, we just call scale_minmax
and provide the object to be recoded. By default, it will recode values to range \([0,1]\).
Let’s check a few things to make sure everything is working as we expect. First, we’ll check to make sure that the range matches the values we specified.
That’s looks good, so let’s check that the correlation is 1.
That also looks good! Finally, let’s check we have the same number of missing values in our new variable.
We’re good to go!
Example 2
Alternatively, we can specify the range for our new variable using the to_min
and to-max
options. A common approach is to recode values to \([-1, 1]\).
Let’s run through our checks again to convince ourselves that everything still works.
[1] -1 1
[1] 1
[1] TRUE
Everything looks good!
Example 3
We can also choose completely random values of min and max if we want to. This example recodes values to range \([1981, 1984]\).
We’ll check everything one last time just to be sure.
[1] 1981 1984
[1] 1
[1] TRUE
Everything still looks good!