Learn R Programming

rearrr (version 0.3.5)

min_max_scale: Scale to a range

Description

lifecycle::badge("experimental")

Scales the values to a range with MinMax scaling.

Usage

min_max_scale(
  x,
  new_min,
  new_max,
  old_min = NULL,
  old_max = NULL,
  na.rm = FALSE
)

Value

Scaled version of `x`.

Arguments

x

Numeric vector to scale.

new_min

Minimum value of target range.

new_max

Maximum value of target range.

old_min

Minimum value of original range.

If NULL, this is the minimum value in `x`.

old_max

Maximum value of original range.

If NULL, this is the maximum value in `x`.

na.rm

Whether missing values should be removed when calculating `old_min` and/or `old_max`.

N.B. Ignored when both `old_min` and `old_max` are NULL.

Author

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other scaling functions: to_unit_length()

Examples

Run this code
# Attach packages
library(rearrr)

# Set seed
set.seed(1)

# Create numeric vector
x <- runif(10)

# Scale
min_max_scale(x, new_min = -1, new_max = 0)
min_max_scale(x, new_min = -1, new_max = 0, old_max = 3)

Run the code above in your browser using DataLab