Table of Contents
In order to scale values between 0 and 1 in R, you can use the “scale” function which is a part of the base R package. This function takes the input vector of values and transforms them to fit within the range of 0 to 1. This can be useful for normalizing data or for creating a standardized comparison between different sets of values. The scaled values will maintain the same relative distances between each other as the original values, but will now be within the desired range. This can be done easily and efficiently in R, making it a valuable tool for data analysis and visualization.
Scale Values Between 0 and 1 in R
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.
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.
Cite this article
stats writer (2024). How can I scale values between 0 and 1 in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-scale-values-between-0-and-1-in-r/
stats writer. "How can I scale values between 0 and 1 in R?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-scale-values-between-0-and-1-in-r/.
stats writer. "How can I scale values between 0 and 1 in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-scale-values-between-0-and-1-in-r/.
stats writer (2024) 'How can I scale values between 0 and 1 in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-scale-values-between-0-and-1-in-r/.
[1] stats writer, "How can I scale values between 0 and 1 in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I scale values between 0 and 1 in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
