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

Python016

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.

数据Datatest

Datatest = Datatest[,-1]就删除第一列

-2就删除第二列

依次类推

例如:

用iris数据

data(iris)

datatest <- iris

扩展资料:

R具有很强的互动性。除了图形输出是在另外的窗口处,它的输入输出窗口都是在同一个窗口进行的,输入语法中如果出现错误会马上在窗口口中得到提示,对以前输入过的命令有记忆功能,可以随时再现、编辑修改以满足用户的需要。

输出的图形可以直接保存为JPG,BMP,PNG等图片格式,还可以直接保存为PDF文件。另外,和其他编程语言和数据库之间有很好的接口。

参考资料来源:百度百科-R语言