简体   繁体   English

如何按行和列对R中的稀疏矩阵进行归一化?

[英]How can I normalize a sparse matrix in R by both rows and columns?

I have created a sparse matrix using the R package "Matrix". 我已经使用R包“ Matrix”创建了一个稀疏矩阵。 The matrix is not square, and its dimensions are 4561 by 68825. 矩阵不是正方形,其尺寸为4561 x 68825。

I'm looking to standardize this matrix so that each value x is equal to x / row sum + column sum. 我正在寻求标准化此矩阵,以便每个值x等于x /行总和+列总和。 I've found a solution on stack which I could alter to solve this problem here . 我在堆栈上找到了一个解决方案,可以在这里解决此问题。 However, in the solution seen in the linked question, the problem uses a square matrix, so Diaganal can be used.In my case, my matrix is not square so I can't make this solution work. 但是,在链接的问题中看到的解决方案中,该问题使用方阵,因此可以使用Diaganal。在我的情况下,我的矩阵不是方阵,因此无法使此解决方案起作用。

How can I normalize a sparse matrix in R by both rows and columns? 如何按行和列对R中的稀疏矩阵进行归一化?

Hope this helps! 希望这可以帮助!

m_final <- t(t(m/rowSums(m)) + rowSums(t(m)))
m_final

Output is: 输出为:

           [,1]     [,2]       [,3]
 [1,] 0.9748283 3.326324 -0.8274075
 [2,] 1.4574957 2.776025 -0.7597753
 [3,] 1.9265464 2.937874 -1.3906749
 [4,] 0.7105211 3.337394 -0.5741696
 [5,] 1.4808831 3.030777 -1.0379153
 [6,] 2.2123599 2.537209 -1.2758243
 [7,] 2.8672471 2.437124 -1.8306263
 [8,] 4.8144351 6.952963 -8.2936531
 [9,] 1.9486587 3.382196 -1.8571098
[10,] 0.8897446 3.329129 -0.7451281


#sample data:
set.seed(1)
m <- replicate(3,rnorm(10))
> m
            [,1]        [,2]        [,3]
 [1,] -0.6264538  1.51178117  0.91897737
 [2,]  0.1836433  0.38984324  0.78213630
 [3,] -0.8356286 -0.62124058  0.07456498
 [4,]  1.5952808 -2.21469989 -1.98935170
 [5,]  0.3295078  1.12493092  0.61982575
 [6,] -0.8204684 -0.04493361 -0.05612874
 [7,]  0.4874291 -0.01619026 -0.15579551
 [8,]  0.7383247  0.94383621 -1.47075238
 [9,]  0.5757814  0.82122120 -0.47815006
[10,] -0.3053884  0.59390132  0.41794156

Edit: 编辑:
In case you want to have below calculation then you can try 如果您想进行以下计算,则可以尝试

m/(row_sum + col_sum) m /(行和+行和)

m/outer(rowSums(m), colSums(m), FUN = "+")

If you simply want to divide each cell by sum of row sum and col sum, here is a simple way to do so: 如果您只是想将每个单元格按行总和和行总和相除,这是一种简单的方法:

test = matrix(1:20, 4, 5)
test
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20

rs = rowSums(test)
cs = colSums(test)

for(j in 1:ncol(test)){
  for(i in 1:nrow(test)){
    test[i,j] = test[i,j]/(rs[i] + cs[j])
  }
}

test
           [,1]       [,2]      [,3]      [,4]      [,5]
[1,] 0.01818182 0.07042254 0.1034483 0.1262136 0.1428571
[2,] 0.03333333 0.07894737 0.1086957 0.1296296 0.1451613
[3,] 0.04615385 0.08641975 0.1134021 0.1327434 0.1472868
[4,] 0.05714286 0.09302326 0.1176471 0.1355932 0.1492537

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

相关问题 R:是否可以通过索引行和列来在循环中填充矩阵? - R: Can a matrix be populated in a loop by indexing both rows and columns? 从r中的稀疏矩阵中提取稀疏行 - extract sparse rows from sparse matrix in r 在R中,当使用命名行时,是否可以将稀疏矩阵列添加(连接)到另一个稀疏矩阵? - In R, when using named rows, can a sparse matrix column be added (concatenated) to another sparse matrix? 您如何规范化R中适当的文档术语矩阵的行? - How do you normalize the rows of a document term matrix in place in R? R - 我可以有一个行列数不同的矩阵吗? - R - Can I have a matrix with different number of columns for rows? 如何通过与 R 中的另一个数据框匹配来选择矩阵中的所有行和列? - How can I select all rows and columns from a matrix by matching with another data frame in R? R package Matrix:获取稀疏矩阵每行/每列的非零条目数 - R package Matrix: get number of non-zero entries per rows / columns of a sparse matrix R中的多个箱形图,同时按列和行将矩阵分组 - Multiple boxplots in R while grouping a matrix both by columns and rows 我可以从SQL Server在R中创建稀疏矩阵吗 - Can I create a sparse matrix in R from SQL Server 更新 R 中稀疏矩阵的单个元素非常慢 - 我怎样才能更快地做到这一点? - Updating individual elements of a sparse matrix in R is very slow - how can I do it faster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM