How to arrange values Between 0 and 1 in R
How to arrange values Between 0 and 1 in R?, The following techniques can be used in R to scale a variable’s values between 0 and 1:
Using the following data frame in R, the following examples demonstrate how to use each technique in practise:
Let’s create a data frame
df <- data.frame(store=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'), sales=c(102, 124, 223, 359, 145, 134, 520, 177))
Now we can view the data frame
Why is Data Management Essential for Data Science? » finnstats
df
store sales 1 A 102 2 B 124 3 C 223 4 D 359 5 E 145 6 F 134 7 G 520 8 H 177
Example 1: Using Base R, Scale Values Between 0 and 1
The code that follows demonstrates how to create a new function in base R and then use it to scale the numbers in the data frame’s sales column to be between 0 and 1:
Scale values between 0 and 1 using the #define function.
scale_values <- function(x){(x-min(x))/(max(x)-min(x))}
Now we can use scale_values in ‘sales’ column to be between 0 and 1
df$sales <- scale_values(df$sales)
Now we can view the updated data frame
df
store sales 1 A 0.00000000 2 B 0.05263158 3 C 0.28947368 4 D 0.61483254 5 E 0.10287081 6 F 0.07655502 7 G 1.00000000 8 H 0.17942584
The sales column’s values are now scaled between 0 and 1.
This function scaled each of the values using the following formula:
Scaled value = (value – min value) / (max value – min value)
Example 2: Using the scales Package to Scale Values Between 0 and 1.
The numbers in the data frame’s sales column can be scaled to be between 0 and 1 by using the rescale() function from the scales package in R, as seen in the code below.
How to Apply AI to Small Data Sets? » finnstats
library(scales)
Let’s scale values in ‘sales’ column to be between 0 and 1
df$sales <- rescale(df$sales)
Now we can view the updated data frame
df
store sales 1 A 0.00000000 2 B 0.05263158 3 C 0.28947368 4 D 0.61483254 5 E 0.10287081 6 F 0.07655502 7 G 1.00000000 8 H 0.17942584
The sales column’s values are now scaled between 0 and 1.
You’ll see that the scaled values correspond to those obtained using the standard R technique.
Recall that the to argument, which is accepted by the rescale() function, determines the scaled values’ range.
Instead, you could scale the values in the sales column to be between 0 and 100 using the syntax shown in the example below:
library(scales)
Now we can scale values in ‘sales’ column to be between 0 and 100
df$sales <- rescale(df$sales, to=c(0,100)) df
store sales 1 A 0.000000 2 B 5.263158 3 C 28.947368 4 D 61.483254 5 E 10.287081 6 F 7.655502 7 G 100.000000 8 H 17.942584
The sales column’s values are now all scaled between 0 and 100.
How to land a job as a Data Scientist in 2022 » finnstats