Normalizes a numeric vector to a range of 0 to 1 using min-max scaling.
Value
A numeric vector with values scaled between 0 and 1. If min_val
and max_val
are equal (or x
has no variance), returns a vector of 0.5s.
Examples
# Normalize a vector
x_vec <- c(10, 20, 30, 40, 50)
normalized_x <- min_max_normalize(x_vec)
print(normalized_x) # Should be 0, 0.25, 0.5, 0.75, 1
#> [1] 0.00 0.25 0.50 0.75 1.00
# Normalize with custom min/max
custom_normalized_x <- min_max_normalize(x_vec, min_val = 0, max_val = 100)
print(custom_normalized_x) # Should be 0.1, 0.2, 0.3, 0.4, 0.5
#> [1] 0.1 0.2 0.3 0.4 0.5