Skip to content

Rescale numeric vector to have specified maximum

Usage

rescale_max(x, to = c(0, 1), from = range(x, na.rm = TRUE))

Arguments

x

numeric vector of values to manipulate.

to

output range (numeric vector of length two)

from

input range (numeric vector of length two). If not given, is calculated from the range of x

Examples

rescale_max(1:100)
#>   [1] 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10 0.11 0.12 0.13
#>  [14] 0.14 0.15 0.16 0.17 0.18 0.19 0.20 0.21 0.22 0.23 0.24 0.25 0.26
#>  [27] 0.27 0.28 0.29 0.30 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39
#>  [40] 0.40 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.50 0.51 0.52
#>  [53] 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.60 0.61 0.62 0.63 0.64 0.65
#>  [66] 0.66 0.67 0.68 0.69 0.70 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78
#>  [79] 0.79 0.80 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 0.90 0.91
#>  [92] 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.00
rescale_max(runif(50))
#>  [1] 0.22995471 0.02293431 0.61905132 0.38239820 0.27435716 0.71588500
#>  [7] 0.66920416 0.53018944 0.68014331 1.00000000 0.20461623 0.62281934
#> [13] 0.49247788 0.94128131 0.21133993 0.99493524 0.88716778 0.94662785
#> [19] 0.86906737 0.57129622 0.41679070 0.67672422 0.26101910 0.08285731
#> [25] 0.89987639 0.89027440 0.82384201 0.39834980 0.74560784 0.17028084
#> [31] 0.22733756 0.17409553 0.80462349 0.84851177 0.80245793 0.45067121
#> [37] 0.90891520 0.45069601 0.57398125 0.49199009 0.76509821 0.31285773
#> [43] 0.17930684 0.31023498 0.92385976 0.04714151 0.82573979 0.55842201
#> [49] 0.29099606 0.44907500
rescale_max(1)
#> [1] 1