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

Python012

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.

在“设置”-“语言格式设置”里面,找到style里面的Line numbermargin一项,调整字体大小就可以调整左边标号的大小,然后文本内容的行间距就可以任意调整。