r语言中如何实现数据标准化(每一列的值除以该列均值)?

Python024

r语言中如何实现数据标准化(每一列的值除以该列均值)?,第1张

使用apply函数apply(mat, 2, function(x)x/(mean(x)))

测试运行结果:

>ma <- matrix(c(1:4, 1, 6:8), nrow = 2)

>ma

[,1] [,2] [,3] [,4]

[1,]1317

[2,]2468

>apply(ma, 2, function(x)x/(mean(x)))

[,1] [,2] [,3] [,4]

[1,] 0.6666667 0.8571429 0.2857143 0.9333333

[2,] 1.3333333 1.1428571 1.7142857 1.0666667

apply函数参数帮助。

apply(X, MARGIN, FUN, ...)

Arguments

X

an array, including a matrix.

MARGIN

a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names.

FUN

the function to be applied: see ‘Details’. In the case of functions like +, %*%, etc., the function name must be backquoted or quoted.

...

optional arguments to FUN.

不慢。

数据标准化,将数据按比例缩放,使之落入一个小的特定区间。去除数据的单位限制,将其转化为无量纲的纯数值,便于不同单位或量级的指标能够进行比较和加权。

所有R的函数和数据集是保存在程序包里面的。只有当一个包被载入时,它的内容才可以被访问。