简体   繁体   English

用rowmean替换所有行

[英]Replace all rows with rowmean

I have a matrix with 4 columns and I have sorted the matrix and calculated the rowMeans for each row. 我有一个包含4列的矩阵,并且已经对该矩阵进行了排序并计算了每一行的rowMeans Now I want to replace all the values in the original matrix with the respective rowMeans . 现在,我想用相应的rowMeans替换原始矩阵中的所有值。 c5sub is the matrix with 4 columns 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

I have used above one to replace the original values with rowMeans but it does not replace the zeros because I have the ifelse . 我已经使用上面的1用rowMeans替换了原始值,但是它没有替换零,因为我有ifelse How do I modify it so that it replaces all the values? 如何修改它以替换所有值?

To keep the original attributes of sortmat such as row and column names: 要保留sortmat的原始属性,例如行和列名称,请执行以下操作:

sortmat[] <- rowMeans(sortmat)

This works because 1) matrices in R are stored in column-major order, meaning all values in column 1, followed by all values in column 2, and so on; 之所以如此,是因为1)R中的矩阵以列优先顺序存储,这意味着列1中的所有值,然后列2中的所有值,依此类推; 2) vectors are recycled, so the vector of rowmeans gets replicated to the correct length for assignment; 2)向量被回收,因此行平均值向量被复制到正确的长度以进行赋值; and 3) assignment with empty brackets on the LHS [] means "replace all elements in an object, but keep the object itself". 3)在LHS []上用方括号括起来的意思是“替换对象中的所有元素,但保留对象本身”。

I think the problem here is in trying to literally replace all of the values, instead of trying to simply create the final matrix the OP desires. 我认为这里的问题是试图从字面上替换所有值,而不是尝试简单地创建OP所需的最终矩阵。

Once the row means have been calculated, and with the size of the final matrix known, we can create the matrix a like so: 一旦该行手段都已计算,并与已知的最终矩阵的大小,我们可以创建矩阵a像这样:

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

Where 'rwmeans' contains an array with the column means, and 'Nc' is the number of columns in the final matrix. 其中“ rwmeans”包含具有列均值的数组,“ Nc”是最终矩阵中的列数。 The values in 'rwmeans' will be repeated for each column of the matrix. 对于矩阵的每一列,将重复“ rwmeans”中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM