R语言数据缩放-1到1

目录

普通scale

-1到1限定范围scale

普通scale

R语言实战:scale()函数 - 知乎 (zhihu.com)

scale(x, center = TRUE, scale = TRUE)

过程:

  1. 对每个变量(列)计算平均值(mean)和标准差(standard deviation)。

  2. 对于每个数据点,将其对应变量的值减去该变量的平均值,然后除以该变量的标准差。

  3. 返回标准化后的数据。

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 maximum

Usage
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

你可能感兴趣的:(肿瘤生信分析,r语言,开发语言)