Skip to contents

Rescales a numeric vector to a specified range using min-max scaling. This is a generalized form of min-max normalization allowing any output range.

Usage

scRange(x, ra)

Arguments

x

Numeric vector. Input values to be scaled.

ra

Numeric vector of length 2. Desired output range (e.g., c(5, 10)).

Value

A numeric vector of the same length as x, scaled to the range ra.

Details

The scaled values are computed as: $$x_{scaled} = \frac{x - \min(x)}{\max(x) - \min(x)} \cdot (r_{max} - r_{min}) + r_{min}$$

See also

Examples

x <- rnorm(20)
plot(x, type = 'l'); abline(h = range(x), lty = 2)
points(scRange(x, ra = c(5, 10)), type = 'l', col = 'red'); abline(h = c(5, 10), col = 'red', lty = 2)