概述
scale()进行数组的标准化,先减去数据集的均值(中心化)再除以数据集的标准差(标准化)。
为了消除量纲对数据结构的影响。
输入:
x<-read.table("C:\Users\Administrator\Desktop\R\data.exam5.2.1.txt",header = TRUE)
std1.x<-scale(x[2:9]);std1.x
执行结果如下:
> std1.x<-scale(x[2:9]);std1.x
x1
x2
x3
[1,]
0.13774798
0.103406512
0.19521746
[2,]
1.72088817
2.344698037
1.35836580
[3,]
0.89085539
0.018444199
2.11317155
[4,] -0.87828274 -0.211025441 -0.09165809
[5,] -1.10796789
0.211025441
0.23704337
[6,] -0.83580771
1.792466805
0.01676315
[7,]
0.05553747 -0.012494458
0.48063857
[8,] -0.81424291
0.508417334
0.54860018
[9,] -1.06819604
0.005592757 -0.67642761
[10,]
3.16508751
1.474465006
2.08959214
[11,]
0.50222831 -0.143959952
0.36186002
[12,]
1.67061878
1.836923275
1.01410633
[13,] -0.15760133 -0.541688290 -0.38673134
[14,]
0.94771907 -0.378665365
1.42443224
[15,] -0.39006239 -0.500658871 -0.92213834
[16,] -0.40457226
1.037944352
0.39487120
[17,] -0.97817905
0.156383013 -0.62847545
[18,] -0.07080875
0.125396758 -0.35706976
[19,] -0.32791357 -0.012780045 -0.30144878
[20,]
1.86953864 -0.979113305
2.23419786
[21,] -0.14098225 -1.730589490 -0.59475909
[22,]
0.03912446 -2.700825781 -0.52432936
[23,]
0.19381162
0.718133832
0.13426356
[24,]
0.07969636 -0.335732029 -1.09177792
[25,] -0.47509729 -0.523458281 -0.96669683
[26,]
0.05810731 -0.764541818 -1.16758464
[27,]
0.39046468 -0.667204042 -1.36468211
[28,] -0.54633751 -0.523505879 -0.47033911
[29,] -0.83637744 -0.382615994 -0.74795918
[30,] -0.86179700 -0.579433452 -1.30483006
[31,] -0.91356950 -0.122826469 -0.12035006
[32,] -0.91363011
0.777821642 -0.88586571
x4
x5
x6
[1,]
0.21387591
0.11507170
0.1925790
[2,]
2.52501220
2.99460313
1.7555223
[3,]
1.18114978
2.36500509
0.1161781
[4,] -0.12152612
0.76562569 -0.3657415
[5,] -0.54198196 -0.16984211 -0.3376082
[6,] -0.03037963
0.21205951 -0.1833739
[7,] -0.77630643
0.98617134 -0.3289654
[8,] -0.97084570
0.86866315 -0.5856415
[9,] -1.28571540
0.26248930 -0.7914106
[10,]
2.39316660
0.87984288
3.0836620
[11,]
0.85671457
0.06802970
0.1050407
[12,]
0.60514782
0.88928032
1.9887165
[13,] -0.61545968 -0.58499254 -0.5574760
[14,]
0.47951676 -0.83680272
0.5941224
[15,]
0.12614132 -1.40062909 -0.8124301
[16,]
0.57474535
0.16100056
0.1543061
[17,] -0.10696461 -0.23600096 -0.6106686
[18,] -0.10075008 -0.72592494 -0.5387420
[19,]
0.22228381 -0.03282988 -0.4037568
[20,]
1.74545380
0.37365747
2.7816636
[21,] -0.46101024 -0.64485977 -0.4906997
[22,] -0.28456623 -0.83017232
0.2641677
[23,]
0.85342453
0.35908994 -0.1914695
[24,] -0.02849089 -0.79145463 -0.2620925
[25,] -0.62837616 -1.55254763 -0.5515854
[26,] -1.74297103 -0.21107645 -0.3267926
[27,] -1.79981573 -1.94800044 -0.5977929
[28,] -0.32666665
0.01484128 -0.5971009
[29,] -0.37492068 -0.53751497 -0.6056149
[30,] -0.49951598 -0.30041752 -0.7282555
[31,] -0.52309465 -0.14196537 -0.6095259
[32,] -0.55727459 -0.37039974 -0.5592142
x7
x8
[1,]
0.196917512
0.05270804
[2,]
2.379697737
2.51243056
[3,]
0.839811267
0.94523785
[4,] -0.701399606 -0.71850377
[5,] -0.372389151 -0.89618410
[6,]
0.022944879
0.98340247
[7,] -0.374686161
0.41042765
[8,] -0.488895149
0.36097372
[9,] -0.612105933 -0.34351319
[10,]
2.937829757
3.47404424
[11,]
0.962794419
0.21842506
[12,]
1.912763219
0.97792631
[13,] -0.132465431 -0.35505525
[14,]
0.398019694
0.20671450
[15,] -0.539325898 -0.47890071
[16,] -0.088615305 -0.21739302
[17,] -0.615541101 -0.43180574
[18,] -0.235313533 -0.91514003
[19,]
0.106030417 -0.30012516
[20,]
1.574502643
0.86478043
[21,] -0.380687358 -0.62355558
[22,] -0.819830126 -1.18448285
[23,]
0.006927891 -0.73661722
[24,] -0.418412124 -0.50653426
[25,] -0.409824204 -0.78548142
[26,] -1.093650262 -1.49300128
[27,] -1.640980127 -0.13297591
[28,] -0.006750700 -0.15673401
[29,] -0.362849317
0.01858735
[30,] -0.579699468 -0.16903431
[31,] -0.766998898 -0.41512451
[32,] -0.697819582 -0.16549587
attr(,"scaled:center")
x1
x2
x3
x4
3514.3941 1020.2750
937.9866
566.6962
x5
x6
x7
x8
675.3134 1237.7553 1234.0022
351.4438
attr(,"scaled:scale")
x1
x2
x3
x4
x5
824.9554 210.0932 226.8928 164.1314 206.6239
x6
x7
x8
621.3277 483.2369 118.6963
执行结果分析:
32组数据,8个变量x1,...,x8
x1
x2
x3
[1,]
0.13774798
0.103406512
0.19521746
[2,]
1.72088817
2.344698037
1.35836580
[3,]
0.89085539
0.018444199
2.11317155
[4,] -0.87828274 -0.211025441 -0.09165809
[5,] -1.10796789
0.211025441
0.23704337
[6,] -0.83580771
1.792466805
0.01676315
[7,]
0.05553747 -0.012494458
0.48063857
[8,] -0.81424291
0.508417334
0.54860018
[9,] -1.06819604
0.005592757 -0.67642761
[10,]
3.16508751
1.474465006
2.08959214
[11,]
0.50222831 -0.143959952
0.36186002
[12,]
1.67061878
1.836923275
1.01410633
[13,] -0.15760133 -0.541688290 -0.38673134
[14,]
0.94771907 -0.378665365
1.42443224
[15,] -0.39006239 -0.500658871 -0.92213834
[16,] -0.40457226
1.037944352
0.39487120
[17,] -0.97817905
0.156383013 -0.62847545
[18,] -0.07080875
0.125396758 -0.35706976
[19,] -0.32791357 -0.012780045 -0.30144878
[20,]
1.86953864 -0.979113305
2.23419786
[21,] -0.14098225 -1.730589490 -0.59475909
[22,]
0.03912446 -2.700825781 -0.52432936
[23,]
0.19381162
0.718133832
0.13426356
[24,]
0.07969636 -0.335732029 -1.09177792
[25,] -0.47509729 -0.523458281 -0.96669683
[26,]
0.05810731 -0.764541818 -1.16758464
[27,]
0.39046468 -0.667204042 -1.36468211
[28,] -0.54633751 -0.523505879 -0.47033911
[29,] -0.83637744 -0.382615994 -0.74795918
[30,] -0.86179700 -0.579433452 -1.30483006
[31,] -0.91356950 -0.122826469 -0.12035006
[32,] -0.91363011
0.777821642 -0.88586571
x4
x5
x6
[1,]
0.21387591
0.11507170
0.1925790
[2,]
2.52501220
2.99460313
1.7555223
[3,]
1.18114978
2.36500509
0.1161781
[4,] -0.12152612
0.76562569 -0.3657415
[5,] -0.54198196 -0.16984211 -0.3376082
[6,] -0.03037963
0.21205951 -0.1833739
[7,] -0.77630643
0.98617134 -0.3289654
[8,] -0.97084570
0.86866315 -0.5856415
[9,] -1.28571540
0.26248930 -0.7914106
[10,]
2.39316660
0.87984288
3.0836620
[11,]
0.85671457
0.06802970
0.1050407
[12,]
0.60514782
0.88928032
1.9887165
[13,] -0.61545968 -0.58499254 -0.5574760
[14,]
0.47951676 -0.83680272
0.5941224
[15,]
0.12614132 -1.40062909 -0.8124301
[16,]
0.57474535
0.16100056
0.1543061
[17,] -0.10696461 -0.23600096 -0.6106686
[18,] -0.10075008 -0.72592494 -0.5387420
[19,]
0.22228381 -0.03282988 -0.4037568
[20,]
1.74545380
0.37365747
2.7816636
[21,] -0.46101024 -0.64485977 -0.4906997
[22,] -0.28456623 -0.83017232
0.2641677
[23,]
0.85342453
0.35908994 -0.1914695
[24,] -0.02849089 -0.79145463 -0.2620925
[25,] -0.62837616 -1.55254763 -0.5515854
[26,] -1.74297103 -0.21107645 -0.3267926
[27,] -1.79981573 -1.94800044 -0.5977929
[28,] -0.32666665
0.01484128 -0.5971009
[29,] -0.37492068 -0.53751497 -0.6056149
[30,] -0.49951598 -0.30041752 -0.7282555
[31,] -0.52309465 -0.14196537 -0.6095259
[32,] -0.55727459 -0.37039974 -0.5592142
x7
x8
[1,]
0.196917512
0.05270804
[2,]
2.379697737
2.51243056
[3,]
0.839811267
0.94523785
[4,] -0.701399606 -0.71850377
[5,] -0.372389151 -0.89618410
[6,]
0.022944879
0.98340247
[7,] -0.374686161
0.41042765
[8,] -0.488895149
0.36097372
[9,] -0.612105933 -0.34351319
[10,]
2.937829757
3.47404424
[11,]
0.962794419
0.21842506
[12,]
1.912763219
0.97792631
[13,] -0.132465431 -0.35505525
[14,]
0.398019694
0.20671450
[15,] -0.539325898 -0.47890071
[16,] -0.088615305 -0.21739302
[17,] -0.615541101 -0.43180574
[18,] -0.235313533 -0.91514003
[19,]
0.106030417 -0.30012516
[20,]
1.574502643
0.86478043
[21,] -0.380687358 -0.62355558
[22,] -0.819830126 -1.18448285
[23,]
0.006927891 -0.73661722
[24,] -0.418412124 -0.50653426
[25,] -0.409824204 -0.78548142
[26,] -1.093650262 -1.49300128
[27,] -1.640980127 -0.13297591
[28,] -0.006750700 -0.15673401
[29,] -0.362849317
0.01858735
[30,] -0.579699468 -0.16903431
[31,] -0.766998898 -0.41512451
[32,] -0.697819582 -0.16549587
以上是数据标准化的结果。
attr(,"scaled:center")
x1
x2
x3
x4
3514.3941 1020.2750
937.9866
566.6962
x5
x6
x7
x8
675.3134 1237.7553 1234.0022
351.4438
数据中心化,数据为每个数据集的平均值。
attr(,"scaled:scale")
x1
x2
x3
x4
x5
824.9554 210.0932 226.8928 164.1314 206.6239
x6
x7
x8
621.3277 483.2369 118.6963
数据标准化,数据为数据集的标准差。
最后
以上就是怡然河马为你收集整理的R语言的scale函数的全部内容,希望文章能够帮你解决R语言的scale函数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复