scale(data, center=T,scale=T),由于默认值均为T,简写成scale(data)即可标准化,data就是你导入的数据集的名称
使用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.
是不一样啊,主成分分析主要运算是求矩阵的特征值和特征向量。cor=T时,输入矩阵为相关系数矩阵,每个元素是0<=x<=1的,对角线为1;
cor=F时,输入矩阵为协方差矩阵,对角线为每个变量的方差;
默认是cor=F的,
而相关系数矩阵就相当于先将数据标准化,然后再求协方差矩阵。
即:先将数据标准化,两种方式才是相同的。否则,cor=T比cor=F相当于多一个标准化过程。