user189594
user189594

Reputation: 191

How to remove columns with all elements zero in a sparse Matrix?

For example , M is a sparse Matrix , and track_list is the colnames of the matrix.

library(Matrix)
M <- Matrix(0,nrow = 3,ncol = 4)
M[1,2] = 1
M[2,3] = 1
M[3,2] = 1 
track_list = c('a','b','c','d')
colnames(M) = track_list

col_tmp <- M@p[-1] - M@p[-length(M@p)]
M <- M[,col_tmp!=0]
track_list = track_list[col_tmp!=0]

And the result will be :

> M
3 x 2 sparse Matrix of class "dgCMatrix"
     b c
[1,] 1 .
[2,] . 1
[3,] 1 .

However , the design is ugly . So How to do that ?

Thank you .

Upvotes: 3

Views: 5794

Answers (2)

Ali
Ali

Reputation: 9830

Try this :

M <- matrix(0,nrow = 3,ncol = 4)
M[1,2] = M[2,3] = M[3,2] = 1
M = M[,colSums(M != 0) != 0]

If you are interested to use Matrix package, you can do exactly as above - just change matrix(...) with Matrix(...). The points are zero values, don't worry about them:

M = Matrix(0,nrow = 3,ncol = 4)
M
# 3 x 4 sparse Matrix of class "dgCMatrix"
# [1,] . . . .
# [2,] . . . .
# [3,] . . . .

M[1,1]
# [1] 0

Actually it seems the Matrix package has optimizations for sparse matrices (matrices of a few non-zero elements). So it shows zeros by points to better represent how sparse is the matrix.

Upvotes: 4

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162321

It might be most straightforward to use summary() to get a sparseSummary containing the indices of columns with non-zero entries.

library(Matrix)
M <- Matrix(c(0,0,0,1,0,0,0,1,1,1,0,0), nc=4)
M[,unique(summary(M)$j)]
# 3 x 3 sparse Matrix of class "dgCMatrix"
#           
# [1,] 1 . 1
# [2,] . 1 .
# [3,] . 1 .

## To see how it works, compare M and summary(M)
M 
# 3 x 4 sparse Matrix of class "dgCMatrix"
#             
# [1,] . 1 . 1
# [2,] . . 1 .
# [3,] . . 1 .

summary(M)
# 3 x 4 sparse Matrix of class "dgCMatrix", with 4 entries 
#   i j x
# 1 1 2 1
# 2 2 3 1
# 3 3 3 1
# 4 1 4 1

Upvotes: 7

Related Questions