目錄
普通scale
-1到1限定范圍scale
普通scale
R語言實戰:scale()函數 - 知乎 (zhihu.com)
scale(x, center = TRUE, scale = TRUE)
過程:
-
對每個變量(列)計算平均值(mean)和標準差(standard deviation)。
-
對于每個數據點,將其對應變量的值減去該變量的平均值,然后除以該變量的標準差。
-
返回標準化后的數據。
rm(list = ls())##模擬數據
df <- runif(20, -10, 10)
df
[1] 9.7741389 -8.8399498 4.7916575 8.0708620 -9.7996628
[6] 5.6400338 8.5961261 -1.7225669 -6.0895427 5.5806776
[11] 9.8484762 8.5260614 -0.5001188 0.8955994 -8.2918119
[16] -4.9564189 6.0818277 7.4064723 -8.1662199 8.3524912
結果:數據超過-1和1
dfscale <- scale(df)
dfscale[1:20]
[1] 1.1244724 -1.4872599 0.4253833 0.8854866 -1.6219167 0.5444185
[7] 0.9591861 -0.4886239 -1.1013518 0.5360902 1.1349026 0.9493554
[13] -0.3171029 -0.1212705 -1.4103510 -0.9423638 0.6064063 0.7922664
[19] -1.3927292 0.9250018
-1到1限定范圍scale
dplyr - how to rescale/normalize data between -1 and 1 in R using groups - Stack Overflow
使用rescale(df,to = c(-1, 1))函數,并可以限制范圍
#數據縮放-1,1之間
library(dplyr)
library(scales)
df1 <- rescale(df,to = c(-1, 1))
df1
[1] 0.99243314 -0.90231003 0.48526233 0.81905521 -1.00000000
[6] 0.57161923 0.87252227 -0.17782586 -0.62234387 0.56557732
[11] 1.00000000 0.86539033 -0.05339187 0.08867942 -0.84651463
[16] -0.50700227 0.61658979 0.75142644 -0.83373052 0.84772247
Rescale continuous vector to have specified minimum and maximumUsage
rescale(x, to, from, ...)x
continuous vector of values to manipulate.to
output range (numeric vector of length two)from
input range (vector of length two). If not given, is calculated from the range of x