Reputation: 323
I have created a sparse matrix using the R package "Matrix". The matrix is not square, and its dimensions are 4561 by 68825.
I'm looking to standardize this matrix so that each value x is equal to x / row sum + column sum. 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.
How can I normalize a sparse matrix in R by both rows and columns?
Upvotes: 0
Views: 1283
Reputation: 11965
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/outer(rowSums(m), colSums(m), FUN = "+")
Upvotes: 1
Reputation: 29742
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
Upvotes: 0