This function rescales a vector of numeric values to an arbitrary range. Optionally, after the stretch values equal to the lowest value can be "nudged" slightly higher to half the minimum value across the rescaled vector of values > 0.
Arguments
- x
Numeric list.
- lower
Numeric, low end of range to which to stretch.
- upper
Numeric, high end of range to which to stretch.
- nudgeUp, nudgeDown
Logical, if
FALSE
(default) then do nothing. IfTRUE
then *after* rescaling to [0, 1], a small value will be added to all values ofx
equal to 0. This value is equal to0.5 * min(x[x > 0])
.- na.rm
Logical, if
FALSE
(default) then if any values ofx
areNA
then the returned value will beNA
. IfTRUE
thenNA
's are ignored in calculation.
Examples
x <- 1:10
stretchMinMax(x)
#> [1] 0.0000000 0.1111111 0.2222222 0.3333333 0.4444444 0.5555556 0.6666667
#> [8] 0.7777778 0.8888889 1.0000000
stretchMinMax(x, lower=2, upper=5)
#> [1] 2.000000 2.333333 2.666667 3.000000 3.333333 3.666667 4.000000 4.333333
#> [9] 4.666667 5.000000
stretchMinMax(x, nudgeUp=TRUE)
#> [1] 0.05555556 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
#> [7] 0.66666667 0.77777778 0.88888889 1.00000000
stretchMinMax(x, lower=2, upper=5, nudgeUp=TRUE)
#> [1] 2.166667 2.333333 2.666667 3.000000 3.333333 3.666667 4.000000 4.333333
#> [9] 4.666667 5.000000
stretchMinMax(x, nudgeDown=TRUE)
#> [1] 0.0000000 0.1111111 0.2222222 0.3333333 0.4444444 0.5555556 0.6666667
#> [8] 0.7777778 0.8888889 0.9444444
stretchMinMax(x, lower=2, upper=5, nudgeUp=TRUE, nudgeDown=TRUE)
#> [1] 2.166667 2.333333 2.666667 3.000000 3.333333 3.666667 4.000000 4.333333
#> [9] 4.666667 4.833333
x <- c(1:5, NA)
stretchMinMax(x)
#> [1] NA NA NA NA NA NA
stretchMinMax(x, na.rm=TRUE)
#> [1] 0.00 0.25 0.50 0.75 1.00 NA