Scale Numeric Vector to a Given Range

scale_to_range(x, min = 0, max = 1, ...)

Arguments

x

Numeric vector

min

Optional. Desired new minimum value. Defaults to 0.

max

Optional. Desired new maximum value. Defaults to 1.

...

Optional. Passed on to min() and max() functions internally. If vector contains missing values, may want to specify na.rm = TRUE.

Value

Numeric vector with minimum value of min and maximum value of max.

Examples

# Simple numeric vector:
x <- c(-2, 0, 4)

# Scale to the c(0, 1) range:
scale_to_range(x)
#> [1] 0.0000000 0.3333333 1.0000000

# Leave minimum value untouched:
scale_to_range(x, min = -2, max = 1)
#> [1] -2 -1  1