scale between 0 and 1 r
Certainly! Here's an example of scaling values between 0 and 1 in R, along with explanations for each step:
# Sample data
values <- c(10, 20, 30, 40, 50)
# Function to scale values between 0 and 1
scale_01 <- function(x) {
(x - min(x)) / (max(x) - min(x))
}
# Applying the scaling function to the values
scaled_values <- scale_01(values)
scaled_values
Explanation:
1. Sample Data: We have a set of values stored in the variable values
.
2. Scaling Function: scale_01
is a custom function created to scale values between 0 and 1. It uses the formula (x - min(x)) / (max(x) - min(x))
where x
represents each value in the dataset.
3. Applying the Function: The scale_01
function is applied to the values
using scaled_values <- scale_01(values)
.
4. Output: The scaled_values
variable now holds the scaled values between 0 and 1.
This process normalizes the values, ensuring they fall within the range of 0 to 1 based on the minimum and maximum values in the dataset.