Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if matrix is sparse matrix

In R, is there a way to tell if a matrix is sparse? There are many sparse matrix classes, for example dgCMatrix, and there is no is.sparseMatrix method.

like image 680
JCWong Avatar asked Aug 22 '26 23:08

JCWong


1 Answers

You could use the "spam" package: as.spam(..) makes an object of class spam out of your matrix and then apply summary(..), witch gives you the density of your matrix or display(..), for a graphical representation of the non-zero entries

testMatrix <-as.spam(matrix(c(123, 2, 0, 0, 0, 2, 23, 0, 0), nrow = 3))
summary(testMatrix)
display(testMatrix) 
like image 163
fluryo Avatar answered Aug 24 '26 15:08

fluryo