R语言中scale函数,可以对数据进行处理,标准化(归一化)在一定的范围,比较适合大范围变化数据归一化处理从而观察数据变化趋势
scale()函数
scale(x, center = TRUE, scale = TRUE)
x一般是一个矩阵,也可以是一个数值向量
center–是否中心化
scale–是否标准化
1、以数值向量为例:
> A
[1] 3.74149 7.36180 5.81734 5.71131 7.97054 10.37620 6.29949 5.55062 5.84779
[10] 15.58810 14.76360 17.74670
长度12,scale=T,center=T:
> scaleA=scale(A)
> scaleA
[,1]
[1,] -1.1123845
[2,] -0.3313828
[3,] -0.6645658
[4,] -0.6874395
[5,] -0.2000606
[6,] 0.3189073
[7,] -0.5605527
[8,] -0.7221048
[9,] -0.6579969
[10,] 1.4432593
[11,] 1.2653916
[12,] 1.9089294
attr(,”scaled:center”)
[1] 8.897915
attr(,”scaled:scale”)
[1] 4.63547
>
长度12,scale=T,center=F,数字都是正数
> scaleA=scale(A,center=F)
> scaleA
[,1]
[1,] 0.3602619
[2,] 0.7088557
[3,] 0.5601421
[4,] 0.5499327
[5,] 0.7674702
[6,] 0.9991073
[7,] 0.6065676
[8,] 0.5344601
[9,] 0.5630741
[10,] 1.5009526
[11,] 1.4215629
[12,] 1.7088007
attr(,”scaled:scale”)
[1] 10.38547
注意:数值不能完全一致,否则返回NaN:
> scale(c(1,1,1,1,1,1))
[,1]
[1,] NaN
[2,] NaN
[3,] NaN
[4,] NaN
[5,] NaN
[6,] NaN
attr(,”scaled: