如何重新缩放连续变量,以使重新缩放的范围在R中变为0到1?
重新调整连续的缩放比例意味着我们要使用一些属性对其进行标准化,并且如果我们使用0到1作为表示该属性的范围,则需要对其进行标准化。在大多数情况下,重新缩放的目的是要使所考虑变量的度量单位的影响为零。要重新缩放以使范围变为0到1,我们可以使用scales包的重新缩放功能。
示例
磅秤包装-
示例
library(scales) x1 <-rnorm(100) x1
输出结果
[1] -0.772550552 -1.151984266 -1.315356809 0.437096362 0.181752921 [6] -0.379936032 -0.805549311 0.670322046 0.203167071 -1.142467229 [11] -0.012974582 -1.439882259 -0.822957144 0.152801238 0.067803942 [16] 1.529766435 -1.103447567 0.001219724 -0.683996137 0.808791613 [21] 1.229964132 1.696962628 1.582712078 1.253337887 -1.217410837 [26] 0.893178806 0.741533535 1.536757016 -0.740341120 1.804874683 [31] -0.389749288 -0.308201671 0.251665969 0.398336133 1.530719157 [36] -1.117013229 -2.016412845 -0.460382654 1.211476063 -1.514138342 [41] -0.232599048 0.518716104 -0.242506539 -0.361625635 -1.837529009 [46] -2.125797749 -1.074633493 0.629513347 0.150809102 0.443929214 [51] -0.961556465 -0.307557618 0.086653083 -0.962782337 0.317680819 [56] -0.019646844 0.972076574 0.237496479 -0.050982053 0.556540426 [61] 0.682788625 2.624155086 0.309248943 -0.011977645 1.006509880 [66] 1.810553565 1.741967705 0.743781501 -1.041031871 0.306839565 [71] 0.100713474 -1.384252517 -1.082314003 0.543702925 -1.204237314 [76] 0.881956363 0.888446919 0.404147642 -0.673194917 0.185389664 [81] -0.638456610 -0.669773375 -0.184540921 1.567653519 2.230218078 [86] 0.634282747 0.238400414 -0.624978624 1.847827377 1.879960265 [91] -0.496729070 -0.542448530 -0.142198693 1.022297016 0.418268919 [96] -0.299177936 -0.419304739 -1.861392173 0.977350535 0.115008163 rescale(sort(x1)) [1] 0.00000000 0.02302863 0.05566488 0.06068876 0.12877168 0.14440469 [7] 0.15611634 0.17062084 0.19124125 0.19401465 0.20501540 0.20701901 [13] 0.21237780 0.21523375 0.21968297 0.22129994 0.22837403 0.24484778 [19] 0.24510586 0.27428496 0.27794980 0.28489698 0.29167798 0.30354020 [25] 0.30581416 0.30653449 0.31312756 0.31596506 0.33333999 0.34296523 [31] 0.35061719 0.35926525 0.36548752 0.36755348 0.37140834 0.38265561 [37] 0.38279120 0.38455536 0.39648630 0.39857211 0.40868971 0.41760395 [43] 0.43680764 0.44340459 0.44480929 0.44501918 0.44779760 0.46181547 [49] 0.46578375 0.46874386 0.47175330 0.47929041 0.47970981 0.48580496 [55] 0.48657060 0.49031325 0.49754057 0.49773087 0.50052365 0.51213926 [61] 0.51264650 0.51442165 0.53140188 0.53262537 0.53559830 0.53956201 [67] 0.54100052 0.55674529 0.56200572 0.56470838 0.58007125 0.58107535 [73] 0.58866264 0.59128721 0.60365469 0.60412795 0.61781442 0.63321768 [79] 0.63458413 0.63558032 0.65219054 0.65330086 0.65943973 0.66276337 [85] 0.70259094 0.70648320 0.71140404 0.76960010 0.76980068 0.77107182 [91] 0.77757641 0.78074666 0.80479965 0.81427450 0.82751820 0.82871377 [97] 0.83656096 0.84332585 0.91706507 1.00000000 x2<-runif(50,2,10) x2 [1] 5.017950 7.426354 2.569811 5.126750 3.672075 9.416323 3.525892 6.214078 [9] 7.077132 8.160106 5.576302 7.548776 8.733092 7.760340 5.359439 2.656515 [17] 4.604550 6.315013 4.104247 6.443923 9.283067 3.129101 7.233028 5.760370 [25] 2.164648 3.650884 2.585623 4.512274 5.930217 6.047945 8.218415 9.394121 [33] 7.709041 6.820809 6.044178 6.632804 2.356585 5.064866 5.255388 7.169543 [41] 2.953712 9.951011 4.179498 8.809468 2.544015 5.726810 4.555995 9.672414 [49] 8.208752 2.579446 rescale(sort(x2)) [1] 0.00000000 0.02465043 0.04872189 0.05203496 0.05327237 0.05406570 [7] 0.06317031 0.10133924 0.12386436 0.17482405 0.19087674 0.19359837 [13] 0.24910205 0.25876648 0.30150475 0.30711982 0.31335580 0.36644855 [19] 0.37247405 0.38042176 0.39694272 0.41030592 0.43815759 0.45748730 [25] 0.46179737 0.48361074 0.49824668 0.49873058 0.52006696 0.53302995 [31] 0.54958580 0.57384374 0.59798926 0.63090870 0.64277706 0.65093036 [37] 0.67575909 0.69148182 0.71206462 0.71865291 0.76999470 0.77624229 [43] 0.77748334 0.84358311 0.85339209 0.91421626 0.92847885 0.93133028 [49] 0.96421985 1.00000000 x3<-rexp(50) x3 [1] 0.43724011 0.57734265 0.16240765 3.06027038 1.69335330 2.04719805 [7] 1.00883080 2.59721485 0.85123118 0.27727613 1.79306890 0.31270181 [13] 1.20950151 0.03838685 0.01057551 0.08253434 2.82416005 0.09150565 [19] 0.73383309 0.73656939 0.21556405 1.02207454 3.12264266 1.30434783 [25] 0.41236434 2.55475368 3.08050034 0.73390612 0.48976916 0.14118507 [31] 0.06027363 1.71559545 0.91614907 4.74737881 1.55898166 0.02348157 [37] 1.52468702 1.27284253 0.23364243 0.64139791 0.68680036 0.14414466 [43] 0.61965855 0.04304207 1.00460086 0.72857546 1.64377723 1.60557884 [49] 0.29733294 3.77812005 rescale(sort(x3)) [1] 0.000000000 0.002724635 0.005871331 0.006854108 0.010491911 0.015191433 [7] 0.017085392 0.027573355 0.028198163 0.032053715 0.043275713 0.047092291 [13] 0.056303925 0.060538176 0.063782742 0.084822780 0.090074376 0.101163933 [19] 0.119651821 0.128585250 0.133174707 0.142759749 0.151579008 0.152688961 [25] 0.152704378 0.153266629 0.177473205 0.191178206 0.209851514 0.210744510 [31] 0.213540434 0.253108672 0.266480775 0.273131949 0.319648382 0.326888420 [37] 0.336725683 0.344789854 0.355255998 0.359951603 0.376307242 0.429957168 [43] 0.537108679 0.546072777 0.593983825 0.643829745 0.648100551 0.656997335 [49] 0.795377028 1.000000000 x4<-rnorm(50,1.5) x4 [1] 2.26666499 2.10707161 1.94570770 2.62580162 1.99484410 1.78952410 [7] 1.63530706 1.03127597 0.27384411 1.50138953 1.40894145 2.97279549 [13] 1.57214139 2.34395171 2.71470005 2.41330833 0.39096945 1.60161246 [19] 4.24676733 2.65075324 0.91532557 1.45155428 4.39103308 0.82168887 [25] 2.33482408 0.03373499 3.83392815 1.52686020 1.52890995 2.11431313 [31] 2.36257519 1.42430353 2.61571846 2.62549168 1.80010657 0.92886292 [37] 1.27955236 2.07264884 1.90563963 0.09203242 1.95782314 2.72227961 [43] 2.58041967 2.19878212 0.83258921 0.79775845 2.06507838 0.67922535 [49] 0.54247623 4.04072818 rescale(sort(x4)) [1] 0.00000000 0.01337926 0.05510505 0.08198532 0.11675612 0.14814005 [7] 0.17534340 0.18083543 0.18333706 0.20232506 0.20543188 0.22893567 [13] 0.28591511 0.31560991 0.31913551 0.32538956 0.33682675 0.34267227 [19] 0.34314268 0.35306430 0.35982791 0.36756082 0.40295364 0.40538231 [25] 0.42960215 0.43879778 0.44157827 0.45007458 0.46619335 0.46793077 [31] 0.47583080 0.47749273 0.49687836 0.51245748 0.52809999 0.53019478 [37] 0.53446887 0.54611213 0.58446419 0.59256526 0.59480821 0.59487934 [43] 0.60060574 0.61528154 0.61702104 0.67451445 0.87214441 0.91960502 [49] 0.96689101 1.00000000