Skip to contents

Normalizes a numeric vector to a range of 0 to 1 using min-max scaling.

Usage

min_max_normalize(x, min_val = NULL, max_val = NULL)

Arguments

x

A numeric vector to be normalized.

min_val

Optional. The minimum value to use for normalization. If NULL, the minimum of x is used.

max_val

Optional. The maximum value to use for normalization. If NULL, the maximum of x is used.

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