You can use the following methods to scale the values of a variable between 0 and 1 in R:
Method 1: Use base R
#define function to scale values between 0 and 1 scale_values <- function(x){(x-min(x))/(max(x)-min(x))} x_scaled <- rescale(x)
Method 2: Use scales Package
library(scales)
x_scaled <- rescale(x)
The following examples show how to use each method in practice with the following data frame in R:
#create data frame df <- data.frame(store=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'), sales=c(12, 24, 23, 59, 45, 34, 50, 77)) #view data frame df store sales 1 A 12 2 B 24 3 C 23 4 D 59 5 E 45 6 F 34 7 G 50 8 H 77
Example 1: Scale Values Between 0 and 1 Using Base R
The following code shows how to define a custom function in base R and then use the function to scale the values in the sales column of the data frame to be between 0 and 1:
#define function to scale values between 0 and 1 scale_values <- function(x){(x-min(x))/(max(x)-min(x))} #scale values in 'sales' column to be between 0 and 1 df$sales <- scale_values(df$sales) #view updated data frame df store sales 1 A 0.0000000 2 B 0.1846154 3 C 0.1692308 4 D 0.7230769 5 E 0.5076923 6 F 0.3384615 7 G 0.5846154 8 H 1.0000000
Each of the values in the sales column are now scaled between 0 and 1.
This function used the following formula to scale each of the values:
- Scaled value = (value – min value) / (max value – min value)
For example, the scaled value for the sales of store A was calculated as:
- Scaled value = (12 – 12) / (77 – 12) = 0 / 65 = 0.
Similarly, the scaled value for the sales of store B was calculated as:
- Scaled value = ( 24 – 12) / (77 – 12) = 12 / 65 = 0.1846.
And so on.
Example 2: Scale Values Between 0 and 1 Using scales Package
The following code shows how to use the rescale() function from the scales package in R to scale the values in the sales column of the data frame to be between 0 and 1:
library(scales) #scale values in 'sales' column to be between 0 and 1 df$sales <- rescale(df$sales) #view updated data frame df store sales 1 A 0.0000000 2 B 0.1846154 3 C 0.1692308 4 D 0.7230769 5 E 0.5076923 6 F 0.3384615 7 G 0.5846154 8 H 1.0000000
Each of the values in the sales column are now scaled between 0 and 1.
Notice that these scaled values match the ones calculated using the base R method.
Also note that the rescale() function accepts a to argument that specifies the range for the scaled values.
For example, you could use the following syntax to instead scale the values in the sales column to be between 0 and 100:
library(scales) #scale values in 'sales' column to be between 0 and 100 df$sales <- rescale(df$sales, to=c(0,100)) #view updated data frame df store sales 1 A 0.00000 2 B 18.46154 3 C 16.92308 4 D 72.30769 5 E 50.76923 6 F 33.84615 7 G 58.46154 8 H 100.00000
Each of the values in the sales column are now scaled between 0 and 100.
Additional Resources
The following tutorials explain how to perform other common tasks in R:
How to Calculate a Trimmed Mean in R
How to Calculate a Weighted Mean in R
How to Calculate the Max Value in Each Row in R