Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 矩阵的列标准化形式_R_Data Analysis - Fatal编程技术网

R 矩阵的列标准化形式

R 矩阵的列标准化形式,r,data-analysis,R,Data Analysis,如何按列标准化矩阵。我正试图达到与scale(矩阵)相同的结果。这是我的实现,当列>1时,它给出了与scale函数不同的结果 standardize <- function(X) { stan <- (X - mean(X))/sd(X) return(stan) } A = matrix( c(2, 4, 3, 5), nrow=2) standardize(A) scale(A) 标准化 我想您可能需要apply()如下所示 或者您可以编写标准化()之类的 其工作

如何按列标准化矩阵。我正试图达到与scale(矩阵)相同的结果。这是我的实现,当列>1时,它给出了与scale函数不同的结果

standardize <- function(X)
{
  stan <- (X - mean(X))/sd(X)
  return(stan)
}
A = matrix( c(2, 4, 3, 5), nrow=2)
standardize(A)
scale(A)
标准化
  • 我想您可能需要
    apply()
    如下所示
  • 或者您可以编写
    标准化()
    之类的
其工作原理与标度(A)相同
  • 我想您可能需要
    apply()
    如下所示
    • 或者您可以编写
      标准化()
      之类的
    其工作原理与
    scale(A)

    apply(A, 2, standardize)
    
    standardize <- function(X) apply(X,2, function(v) (v - mean(v))/sd(v))
    
    standardize(A)