Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.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
用rowmean替换所有行_R - Fatal编程技术网

用rowmean替换所有行

用rowmean替换所有行,r,R,我有一个包含4列的矩阵,我对矩阵进行了排序,并计算了每行的rowMeans。现在,我想用相应的rowMeans替换原始矩阵中的所有值c5sub是具有4列的矩阵 sortmat<-apply(c5sub, 2, sort) # sorted matrix by column [1,] -7 -6 -17 -6 [2,]

我有一个包含4列的矩阵,我对矩阵进行了排序,并计算了每行的
rowMeans
。现在,我想用相应的
rowMeans
替换原始矩阵中的所有值
c5sub
是具有4列的矩阵

sortmat<-apply(c5sub, 2, sort) # sorted matrix by column
[1,]                     -7                     -6                    -17                     -6
[2,]                     -7                     -6                     -9                     -6
[3,]                     -6                     -5                     -8                     -6
[4,]                     -6                     -5                     -8                     -6
[5,]                     -6                     -5                     -7                     -6
[6,]                     -6                     -5                     -7                     -5

rwmeans<-apply(sortmat, 1, mean)# calculated rowmeans 
-9.00 -7.00 -6.25 -6.25 -6.00 -5.75

(a <- sweep(a,1,rwmeans,function(x,y) ifelse(x!=0,y,0)))
[1,]                  -9.00                  -9.00                  -9.00                  -9.00
[2,]                  -7.00                  -7.00                  -7.00                  -7.00
[3,]                  -6.25                  -6.25                  -6.25                  -6.25
[4,]                  -6.25                  -6.25                  -6.25                  -6.25
[5,]                  -6.00                  -6.00                  -6.00                  -6.00
[6,]                  -5.75                  -5.75                  -5.75                  -5.75

sortmat我认为这里的问题在于试图替换所有的值,而不是试图简单地创建OP想要的最终矩阵

一旦计算了行平均值,并且最终矩阵的大小已知,我们就可以创建矩阵
a
,如下所示:

a <- matrix(rwmeans, nrow=length(rwmeans), ncol=Nc)

a要保留
sortmat
的原始属性,如行和列名:

sortmat[] <- rowMeans(sortmat)

sortmat[]如果您试图对矩阵进行分位数规格化,您可能希望查看
affy
包中的
normalize.quantiles
:我尝试过,但我丢失了列名称,als得到了这个outputh>head(h)[,1][,2][,3][,4][1,]-9.00 19.5 29.75 110.25[2,]-7.00 19.5 29.75 110.25[3,] -6.25 19.5 29.75 110.25 [4,] -6.25 19.5 29.75 110.25 [5,] -6.00 19.5 29.75 110.25 [6,] -5.75 19.5 29.75 110.25 >