This function allows you to scale vectors or an entire data frame using the max-min scaling method A numeric vector is always returned.
maxmin(x)
library(magrittr)
library(dplyr)
rand <- rnorm(100, mean = 0, sd = 1)
data.frame(original = rand, transformed = maxmin(rand))
#> original transformed
#> 1 0.544486103 0.7066675
#> 2 0.461272735 0.6891376
#> 3 -1.487765109 0.2785506
#> 4 -0.691952682 0.4461976
#> 5 0.006885965 0.5934159
#> 6 -0.154013286 0.5595206
#> 7 -0.058929590 0.5795511
#> 8 -0.532580725 0.4797711
#> 9 1.079072266 0.8192842
#> 10 0.874615307 0.7762130
#> 11 -0.666018439 0.4516609
#> 12 -1.126275927 0.3547024
#> 13 -0.369435645 0.5141394
#> 14 -1.181595059 0.3430488
#> 15 0.059317620 0.6044612
#> 16 -1.831619941 0.2061136
#> 17 0.813739647 0.7633888
#> 18 -1.484822319 0.2791705
#> 19 -1.424922124 0.2917892
#> 20 1.936921946 1.0000000
#> 21 0.688422563 0.7369894
#> 22 0.324335193 0.6602902
#> 23 -0.138836622 0.5627178
#> 24 -1.183645124 0.3426169
#> 25 1.612366609 0.9316287
#> 26 -0.643163786 0.4564755
#> 27 -1.165716088 0.3463939
#> 28 0.313530113 0.6580140
#> 29 0.548406090 0.7074933
#> 30 -1.257388469 0.3270820
#> 31 1.000885458 0.8028132
#> 32 0.313166147 0.6579373
#> 33 -0.980530030 0.3854054
#> 34 -1.065758393 0.3674511
#> 35 -1.013986765 0.3783574
#> 36 -1.366631361 0.3040688
#> 37 -1.767504531 0.2196203
#> 38 -0.565150868 0.4729098
#> 39 -0.258543887 0.5375001
#> 40 1.631227565 0.9356020
#> 41 0.324885119 0.6604060
#> 42 -1.057615517 0.3691665
#> 43 -1.365557250 0.3042951
#> 44 0.355476659 0.6668505
#> 45 -0.672844030 0.4502230
#> 46 0.403145968 0.6768926
#> 47 0.333291660 0.6621770
#> 48 1.477891118 0.9032999
#> 49 -0.011129994 0.5896206
#> 50 -0.678476313 0.4490365
#> 51 -0.358565398 0.5164294
#> 52 0.601616617 0.7187027
#> 53 -0.750484984 0.4338671
#> 54 -0.396539336 0.5084297
#> 55 -1.926524635 0.1861209
#> 56 -2.205903683 0.1272665
#> 57 1.432228406 0.8936805
#> 58 -0.239728922 0.5414636
#> 59 -0.029360222 0.5857802
#> 60 -2.810031833 0.0000000
#> 61 -0.694158894 0.4457328
#> 62 0.131521920 0.6196719
#> 63 0.327751356 0.6610098
#> 64 1.436151593 0.8945070
#> 65 -1.328722309 0.3120548
#> 66 0.175713636 0.6289814
#> 67 0.526609210 0.7029015
#> 68 -0.172499556 0.5556263
#> 69 0.167689637 0.6272910
#> 70 1.176533063 0.8398154
#> 71 1.525468782 0.9133227
#> 72 -0.564557686 0.4730348
#> 73 0.296297717 0.6543838
#> 74 -0.249213034 0.5394657
#> 75 -0.207099603 0.5483374
#> 76 0.822056628 0.7651409
#> 77 0.703638224 0.7401947
#> 78 -0.157714759 0.5587409
#> 79 -0.176803539 0.5547196
#> 80 1.422535845 0.8916387
#> 81 -1.316708410 0.3145856
#> 82 -0.061337713 0.5790438
#> 83 0.095596030 0.6121037
#> 84 -0.542940416 0.4775887
#> 85 0.498453635 0.6969702
#> 86 -0.041877501 0.5831433
#> 87 0.495870854 0.6964261
#> 88 0.900931706 0.7817568
#> 89 1.619379507 0.9331061
#> 90 0.378967153 0.6717990
#> 91 -0.168917484 0.5563809
#> 92 -0.542436733 0.4776948
#> 93 -1.453846433 0.2856959
#> 94 0.180349350 0.6299579
#> 95 0.497889684 0.6968514
#> 96 -1.293158928 0.3195466
#> 97 0.754753609 0.7509627
#> 98 -0.267332501 0.5356486
#> 99 0.642080562 0.7272269
#> 100 -0.417326371 0.5040507
iris %>% mutate(Petal.Length2 = maxmin(Petal.Length))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species Petal.Length2
#> 1 5.1 3.5 1.4 0.2 setosa 0.06779661
#> 2 4.9 3.0 1.4 0.2 setosa 0.06779661
#> 3 4.7 3.2 1.3 0.2 setosa 0.05084746
#> 4 4.6 3.1 1.5 0.2 setosa 0.08474576
#> 5 5.0 3.6 1.4 0.2 setosa 0.06779661
#> 6 5.4 3.9 1.7 0.4 setosa 0.11864407
#> 7 4.6 3.4 1.4 0.3 setosa 0.06779661
#> 8 5.0 3.4 1.5 0.2 setosa 0.08474576
#> 9 4.4 2.9 1.4 0.2 setosa 0.06779661
#> 10 4.9 3.1 1.5 0.1 setosa 0.08474576
#> 11 5.4 3.7 1.5 0.2 setosa 0.08474576
#> 12 4.8 3.4 1.6 0.2 setosa 0.10169492
#> 13 4.8 3.0 1.4 0.1 setosa 0.06779661
#> 14 4.3 3.0 1.1 0.1 setosa 0.01694915
#> 15 5.8 4.0 1.2 0.2 setosa 0.03389831
#> 16 5.7 4.4 1.5 0.4 setosa 0.08474576
#> 17 5.4 3.9 1.3 0.4 setosa 0.05084746
#> 18 5.1 3.5 1.4 0.3 setosa 0.06779661
#> 19 5.7 3.8 1.7 0.3 setosa 0.11864407
#> 20 5.1 3.8 1.5 0.3 setosa 0.08474576
#> 21 5.4 3.4 1.7 0.2 setosa 0.11864407
#> 22 5.1 3.7 1.5 0.4 setosa 0.08474576
#> 23 4.6 3.6 1.0 0.2 setosa 0.00000000
#> 24 5.1 3.3 1.7 0.5 setosa 0.11864407
#> 25 4.8 3.4 1.9 0.2 setosa 0.15254237
#> 26 5.0 3.0 1.6 0.2 setosa 0.10169492
#> 27 5.0 3.4 1.6 0.4 setosa 0.10169492
#> 28 5.2 3.5 1.5 0.2 setosa 0.08474576
#> 29 5.2 3.4 1.4 0.2 setosa 0.06779661
#> 30 4.7 3.2 1.6 0.2 setosa 0.10169492
#> 31 4.8 3.1 1.6 0.2 setosa 0.10169492
#> 32 5.4 3.4 1.5 0.4 setosa 0.08474576
#> 33 5.2 4.1 1.5 0.1 setosa 0.08474576
#> 34 5.5 4.2 1.4 0.2 setosa 0.06779661
#> 35 4.9 3.1 1.5 0.2 setosa 0.08474576
#> 36 5.0 3.2 1.2 0.2 setosa 0.03389831
#> 37 5.5 3.5 1.3 0.2 setosa 0.05084746
#> 38 4.9 3.6 1.4 0.1 setosa 0.06779661
#> 39 4.4 3.0 1.3 0.2 setosa 0.05084746
#> 40 5.1 3.4 1.5 0.2 setosa 0.08474576
#> 41 5.0 3.5 1.3 0.3 setosa 0.05084746
#> 42 4.5 2.3 1.3 0.3 setosa 0.05084746
#> 43 4.4 3.2 1.3 0.2 setosa 0.05084746
#> 44 5.0 3.5 1.6 0.6 setosa 0.10169492
#> 45 5.1 3.8 1.9 0.4 setosa 0.15254237
#> 46 4.8 3.0 1.4 0.3 setosa 0.06779661
#> 47 5.1 3.8 1.6 0.2 setosa 0.10169492
#> 48 4.6 3.2 1.4 0.2 setosa 0.06779661
#> 49 5.3 3.7 1.5 0.2 setosa 0.08474576
#> 50 5.0 3.3 1.4 0.2 setosa 0.06779661
#> 51 7.0 3.2 4.7 1.4 versicolor 0.62711864
#> 52 6.4 3.2 4.5 1.5 versicolor 0.59322034
#> 53 6.9 3.1 4.9 1.5 versicolor 0.66101695
#> 54 5.5 2.3 4.0 1.3 versicolor 0.50847458
#> 55 6.5 2.8 4.6 1.5 versicolor 0.61016949
#> 56 5.7 2.8 4.5 1.3 versicolor 0.59322034
#> 57 6.3 3.3 4.7 1.6 versicolor 0.62711864
#> 58 4.9 2.4 3.3 1.0 versicolor 0.38983051
#> 59 6.6 2.9 4.6 1.3 versicolor 0.61016949
#> 60 5.2 2.7 3.9 1.4 versicolor 0.49152542
#> 61 5.0 2.0 3.5 1.0 versicolor 0.42372881
#> 62 5.9 3.0 4.2 1.5 versicolor 0.54237288
#> 63 6.0 2.2 4.0 1.0 versicolor 0.50847458
#> 64 6.1 2.9 4.7 1.4 versicolor 0.62711864
#> 65 5.6 2.9 3.6 1.3 versicolor 0.44067797
#> 66 6.7 3.1 4.4 1.4 versicolor 0.57627119
#> 67 5.6 3.0 4.5 1.5 versicolor 0.59322034
#> 68 5.8 2.7 4.1 1.0 versicolor 0.52542373
#> 69 6.2 2.2 4.5 1.5 versicolor 0.59322034
#> 70 5.6 2.5 3.9 1.1 versicolor 0.49152542
#> 71 5.9 3.2 4.8 1.8 versicolor 0.64406780
#> 72 6.1 2.8 4.0 1.3 versicolor 0.50847458
#> 73 6.3 2.5 4.9 1.5 versicolor 0.66101695
#> 74 6.1 2.8 4.7 1.2 versicolor 0.62711864
#> 75 6.4 2.9 4.3 1.3 versicolor 0.55932203
#> 76 6.6 3.0 4.4 1.4 versicolor 0.57627119
#> 77 6.8 2.8 4.8 1.4 versicolor 0.64406780
#> 78 6.7 3.0 5.0 1.7 versicolor 0.67796610
#> 79 6.0 2.9 4.5 1.5 versicolor 0.59322034
#> 80 5.7 2.6 3.5 1.0 versicolor 0.42372881
#> 81 5.5 2.4 3.8 1.1 versicolor 0.47457627
#> 82 5.5 2.4 3.7 1.0 versicolor 0.45762712
#> 83 5.8 2.7 3.9 1.2 versicolor 0.49152542
#> 84 6.0 2.7 5.1 1.6 versicolor 0.69491525
#> 85 5.4 3.0 4.5 1.5 versicolor 0.59322034
#> 86 6.0 3.4 4.5 1.6 versicolor 0.59322034
#> 87 6.7 3.1 4.7 1.5 versicolor 0.62711864
#> 88 6.3 2.3 4.4 1.3 versicolor 0.57627119
#> 89 5.6 3.0 4.1 1.3 versicolor 0.52542373
#> 90 5.5 2.5 4.0 1.3 versicolor 0.50847458
#> 91 5.5 2.6 4.4 1.2 versicolor 0.57627119
#> 92 6.1 3.0 4.6 1.4 versicolor 0.61016949
#> 93 5.8 2.6 4.0 1.2 versicolor 0.50847458
#> 94 5.0 2.3 3.3 1.0 versicolor 0.38983051
#> 95 5.6 2.7 4.2 1.3 versicolor 0.54237288
#> 96 5.7 3.0 4.2 1.2 versicolor 0.54237288
#> 97 5.7 2.9 4.2 1.3 versicolor 0.54237288
#> 98 6.2 2.9 4.3 1.3 versicolor 0.55932203
#> 99 5.1 2.5 3.0 1.1 versicolor 0.33898305
#> 100 5.7 2.8 4.1 1.3 versicolor 0.52542373
#> 101 6.3 3.3 6.0 2.5 virginica 0.84745763
#> 102 5.8 2.7 5.1 1.9 virginica 0.69491525
#> 103 7.1 3.0 5.9 2.1 virginica 0.83050847
#> 104 6.3 2.9 5.6 1.8 virginica 0.77966102
#> 105 6.5 3.0 5.8 2.2 virginica 0.81355932
#> 106 7.6 3.0 6.6 2.1 virginica 0.94915254
#> 107 4.9 2.5 4.5 1.7 virginica 0.59322034
#> 108 7.3 2.9 6.3 1.8 virginica 0.89830508
#> 109 6.7 2.5 5.8 1.8 virginica 0.81355932
#> 110 7.2 3.6 6.1 2.5 virginica 0.86440678
#> 111 6.5 3.2 5.1 2.0 virginica 0.69491525
#> 112 6.4 2.7 5.3 1.9 virginica 0.72881356
#> 113 6.8 3.0 5.5 2.1 virginica 0.76271186
#> 114 5.7 2.5 5.0 2.0 virginica 0.67796610
#> 115 5.8 2.8 5.1 2.4 virginica 0.69491525
#> 116 6.4 3.2 5.3 2.3 virginica 0.72881356
#> 117 6.5 3.0 5.5 1.8 virginica 0.76271186
#> 118 7.7 3.8 6.7 2.2 virginica 0.96610169
#> 119 7.7 2.6 6.9 2.3 virginica 1.00000000
#> 120 6.0 2.2 5.0 1.5 virginica 0.67796610
#> 121 6.9 3.2 5.7 2.3 virginica 0.79661017
#> 122 5.6 2.8 4.9 2.0 virginica 0.66101695
#> 123 7.7 2.8 6.7 2.0 virginica 0.96610169
#> 124 6.3 2.7 4.9 1.8 virginica 0.66101695
#> 125 6.7 3.3 5.7 2.1 virginica 0.79661017
#> 126 7.2 3.2 6.0 1.8 virginica 0.84745763
#> 127 6.2 2.8 4.8 1.8 virginica 0.64406780
#> 128 6.1 3.0 4.9 1.8 virginica 0.66101695
#> 129 6.4 2.8 5.6 2.1 virginica 0.77966102
#> 130 7.2 3.0 5.8 1.6 virginica 0.81355932
#> 131 7.4 2.8 6.1 1.9 virginica 0.86440678
#> 132 7.9 3.8 6.4 2.0 virginica 0.91525424
#> 133 6.4 2.8 5.6 2.2 virginica 0.77966102
#> 134 6.3 2.8 5.1 1.5 virginica 0.69491525
#> 135 6.1 2.6 5.6 1.4 virginica 0.77966102
#> 136 7.7 3.0 6.1 2.3 virginica 0.86440678
#> 137 6.3 3.4 5.6 2.4 virginica 0.77966102
#> 138 6.4 3.1 5.5 1.8 virginica 0.76271186
#> 139 6.0 3.0 4.8 1.8 virginica 0.64406780
#> 140 6.9 3.1 5.4 2.1 virginica 0.74576271
#> 141 6.7 3.1 5.6 2.4 virginica 0.77966102
#> 142 6.9 3.1 5.1 2.3 virginica 0.69491525
#> 143 5.8 2.7 5.1 1.9 virginica 0.69491525
#> 144 6.8 3.2 5.9 2.3 virginica 0.83050847
#> 145 6.7 3.3 5.7 2.5 virginica 0.79661017
#> 146 6.7 3.0 5.2 2.3 virginica 0.71186441
#> 147 6.3 2.5 5.0 1.9 virginica 0.67796610
#> 148 6.5 3.0 5.2 2.0 virginica 0.71186441
#> 149 6.2 3.4 5.4 2.3 virginica 0.74576271
#> 150 5.9 3.0 5.1 1.8 virginica 0.69491525
maxmin(iris$Petal.Length)
#> [1] 0.06779661 0.06779661 0.05084746 0.08474576 0.06779661 0.11864407
#> [7] 0.06779661 0.08474576 0.06779661 0.08474576 0.08474576 0.10169492
#> [13] 0.06779661 0.01694915 0.03389831 0.08474576 0.05084746 0.06779661
#> [19] 0.11864407 0.08474576 0.11864407 0.08474576 0.00000000 0.11864407
#> [25] 0.15254237 0.10169492 0.10169492 0.08474576 0.06779661 0.10169492
#> [31] 0.10169492 0.08474576 0.08474576 0.06779661 0.08474576 0.03389831
#> [37] 0.05084746 0.06779661 0.05084746 0.08474576 0.05084746 0.05084746
#> [43] 0.05084746 0.10169492 0.15254237 0.06779661 0.10169492 0.06779661
#> [49] 0.08474576 0.06779661 0.62711864 0.59322034 0.66101695 0.50847458
#> [55] 0.61016949 0.59322034 0.62711864 0.38983051 0.61016949 0.49152542
#> [61] 0.42372881 0.54237288 0.50847458 0.62711864 0.44067797 0.57627119
#> [67] 0.59322034 0.52542373 0.59322034 0.49152542 0.64406780 0.50847458
#> [73] 0.66101695 0.62711864 0.55932203 0.57627119 0.64406780 0.67796610
#> [79] 0.59322034 0.42372881 0.47457627 0.45762712 0.49152542 0.69491525
#> [85] 0.59322034 0.59322034 0.62711864 0.57627119 0.52542373 0.50847458
#> [91] 0.57627119 0.61016949 0.50847458 0.38983051 0.54237288 0.54237288
#> [97] 0.54237288 0.55932203 0.33898305 0.52542373 0.84745763 0.69491525
#> [103] 0.83050847 0.77966102 0.81355932 0.94915254 0.59322034 0.89830508
#> [109] 0.81355932 0.86440678 0.69491525 0.72881356 0.76271186 0.67796610
#> [115] 0.69491525 0.72881356 0.76271186 0.96610169 1.00000000 0.67796610
#> [121] 0.79661017 0.66101695 0.96610169 0.66101695 0.79661017 0.84745763
#> [127] 0.64406780 0.66101695 0.77966102 0.81355932 0.86440678 0.91525424
#> [133] 0.77966102 0.69491525 0.77966102 0.86440678 0.77966102 0.76271186
#> [139] 0.64406780 0.74576271 0.77966102 0.69491525 0.69491525 0.83050847
#> [145] 0.79661017 0.71186441 0.67796610 0.71186441 0.74576271 0.69491525