Performs a linear transformation of numeric data to a specified range.
Each column of x is rescaled independently.
Value
an object of the same shape as x: a numeric vector for
vector input, otherwise a numeric matrix with the same dimensions,
where each column is linearly rescaled to the interval
[newLow, newHigh].
Details
The transformation is defined as: $$ x_{scaled} = \frac{x - low}{high - low} \cdot (newHigh - newLow) + newLow $$
Constant columns (where high == low) are mapped to newLow.
If low and high are supplied, values of x outside
[low, high] are extrapolated linearly and are not clipped to the
target range.
See also
scale(), DescToolsX::scaleX
Other math.transform:
logit(),
percentRank(),
rankX(),
winsorize()
Examples
x <- matrix(1:10, ncol = 2)
# default scaling to [0,1]
linScale(x)
#> [,1] [,2]
#> [1,] 0.00 0.00
#> [2,] 0.25 0.25
#> [3,] 0.50 0.50
#> [4,] 0.75 0.75
#> [5,] 1.00 1.00
# custom range
linScale(x, newLow = -1, newHigh = 1)
#> [,1] [,2]
#> [1,] -1.0 -1.0
#> [2,] -0.5 -0.5
#> [3,] 0.0 0.0
#> [4,] 0.5 0.5
#> [5,] 1.0 1.0
# using predefined bounds
linScale(x, low = 1, high = 10)
#> [,1] [,2]
#> [1,] 0.0000000 0.5555556
#> [2,] 0.1111111 0.6666667
#> [3,] 0.2222222 0.7777778
#> [4,] 0.3333333 0.8888889
#> [5,] 0.4444444 1.0000000
