linear_map: Linearly map numbers

Description Usage Arguments Details Examples

View source: R/linear_map.R

Description

Linearly map (scale) one or more numbers from one range to another range. This is the same as linear interpolation.

Usage

1
2
3
4
5
6
7
linear_map(
  x,
  in_start = min(x, na.rm = TRUE),
  in_end = max(x, na.rm = TRUE),
  out_start = 0,
  out_end = 1
)

Arguments

x

A numeric vector.

in_start, in_end

The limits of the input range, defaults to min(x) and max(x) with NA removed.

out_start, out_end

The limits of the output range, defaults to 0 and 1.

Details

If the input is outside the input range then it will be outside the output range, too.

Can also map to ranges where start > end (see examples).

Examples

1
2
3
4
5
6
7
8
linear_map(1:3)                                # 0, 0.5, 1
linear_map(1:3, 0, 10)                         # 0.1, 0.2, 0.3
linear_map(1:3, 1, 10)                         # 0, 0.11, 0.22
linear_map(1:3, 0, 10, 0, 100)                 # 10, 20, 30
linear_map(-1, 0, 1, 20, 30)                   # 10

# Ranges may also go from high to low values (inverts the result):
linear_map(1:3, out_start = 30, out_end = 10)  # 30, 20, 10)

BastiHz/BastiHzR documentation built on March 24, 2021, 1:48 p.m.