Skip to contents

make_scale() will rescale any vector to have a user-defined minimum and maximum.

Usage

make_scale(x, minim, maxim)

Arguments

x

a numeric vector

minim

a desired numeric minimum

maxim

a desired numeric maximum

Value

The function takes a numeric vector and returns a rescaled version of it with the observed (desired) minimum, the observed (desired) maximum, and rescaled values between both extremes.

Details

This function is useful if you wanted to do some kind of minimum-maximum rescaling of a variable on some given scale, prominently rescaling to a minimum of 0 and a maximum of 1 (thinking ahead to a regression). The function is flexible enough for any minimum or maximum.

Examples


x <- runif(100, 1, 100)
make_scale(x, 2, 5) # works
#>   [1] 3.691435 2.107502 3.670319 2.808087 2.615673 2.354313 2.035948 3.128916
#>   [9] 3.787428 2.048508 2.514866 3.244003 2.778355 4.685685 4.348520 3.125769
#>  [17] 4.614064 3.065012 3.651240 4.612916 4.896847 2.237435 4.184424 3.979520
#>  [25] 3.335907 2.936188 2.354956 2.975470 3.351972 4.891430 4.500238 3.191101
#>  [33] 3.957162 4.667745 2.537577 2.117305 2.272307 4.773745 3.803732 4.907440
#>  [41] 3.106993 2.655200 4.768845 3.331101 3.252278 4.399584 2.000000 2.862249
#>  [49] 2.534993 4.341187 4.848127 2.970642 2.520859 2.757290 2.593809 3.852103
#>  [57] 4.917708 2.434919 2.254518 2.785304 3.631706 2.729471 4.742084 4.935707
#>  [65] 4.232052 3.276069 3.551173 3.827683 4.342697 4.850768 2.397893 2.485624
#>  [73] 4.386657 4.911031 4.597821 2.292243 3.615681 4.947668 2.276199 2.769858
#>  [81] 3.763996 4.694063 4.213002 4.505863 2.531546 4.016657 2.649579 3.645823
#>  [89] 4.208062 2.575016 4.158535 4.681841 4.639911 3.903428 3.686708 2.344965
#>  [97] 3.054974 3.248985 5.000000 4.889466
make_scale(x, 5, 2) # results in message
#> The desired minimum should not be greater than or equal to the desired maximum. Try again.
make_scale(x, 0, 1) # probably why you're using this.
#>   [1] 0.56381179 0.03583408 0.55677310 0.26936218 0.20522434 0.11810428
#>   [7] 0.01198278 0.37630528 0.59580925 0.01616923 0.17162203 0.41466759
#>  [13] 0.25945173 0.89522844 0.78283997 0.37525631 0.87135473 0.35500402
#>  [19] 0.55041347 0.87097216 0.96561569 0.07914503 0.72814125 0.65983986
#>  [25] 0.44530222 0.31206271 0.11831883 0.32515659 0.45065717 0.96380997
#>  [31] 0.83341280 0.39703383 0.65238741 0.88924841 0.17919234 0.03910175
#>  [37] 0.09076887 0.92458181 0.60124387 0.96914665 0.36899770 0.21839989
#>  [43] 0.92294842 0.44370037 0.41742612 0.79986144 0.00000000 0.28741634
#>  [49] 0.17833088 0.78039566 0.94937583 0.32354748 0.17361974 0.25243013
#>  [55] 0.19793622 0.61736765 0.97256942 0.14497303 0.08483947 0.26176803
#>  [61] 0.54390194 0.24315704 0.91402786 0.97856894 0.74401739 0.42535642
#>  [67] 0.51705782 0.60922758 0.78089907 0.95025615 0.13263085 0.16187452
#>  [73] 0.79555246 0.97034368 0.86594029 0.09741447 0.53856031 0.98255591
#>  [79] 0.09206622 0.25661945 0.58799873 0.89802115 0.73766743 0.83528771
#>  [85] 0.17718193 0.67221891 0.21652650 0.54860782 0.73602058 0.19167198
#>  [91] 0.71951174 0.89394686 0.87997035 0.63447593 0.56223588 0.11498833
#>  [97] 0.35165797 0.41632849 1.00000000 0.96315523