Reputation: 49
I havea very large data set represented as a sparse matrix in a text file (three columns, i, j , value). There are two seperate analyses I am attempting - first, to find the average value of each column and second, to find all diagonal elements of the matrix.
With previous data sets representing the same information in a less granular fashion, I just used normal matrix functions. To do the same for this sparse matrix, I wanted to reconstruct the dense matrix and calculate in the same manner. However, if there is a way to get the column averages / diagonals without reconstructing the dense matrix, that works as well.
Upvotes: 1
Views: 154
Reputation: 887481
We can use colMeans
and diag
colMeans(m1)
#[1] 0.0 0.6 2.8 0.0 1.4 0.0 2.8 0.0 2.4
diag(m1)
#[1] 0 0 9 0 0
library(Matrix)
m1 <- sparseMatrix(c(1,1,2,3, 4,5), c(2,5, 3,3,7,9),
x = c(3,7, 5, 9, 14, 12))
Upvotes: 3