Reputation: 23
I have a sparseMatrix in R as implemented in the Matrix package. I'd like to perform some operations on the column numbers of non-zero entries. Based on how it's stored and printed it seems there should be an easy function to do this but I can't find one in the docs. As a toy example where f(Matrix, Int) is the function I'd like,
M <- Matrix(c(1,0,0,1,0,1,0,0), nrow = 2)
# 1 0 0 1
# 0 1 0 0
f(M,1) = [1,4]
f(M,2) = [2]
So given a matrix and row number we get returned a list of indices of non zero values. Note: I don't want to iterate over all columns of the data set (there are many millions of them), I'd like some way to get at how the sparseMatrix is stored in memory or an inbuilt efficient conversion to a List of Lists or Coordinate List forms.
Upvotes: 1
Views: 516
Reputation: 4595
Did you try str(M)
? it will show matrix actually keeps data.
By default it stored in column-major format - dgCMatrix
. But you can convert it to triplet (coordinate list) and row-major format with as()
function.
M_triplet = as(M, 'TsparseMatrix')
M_row = as(M, 'RsparseMatrix')
Then you can perform all your operations elementwise and convert it back to dgCMatrix
(which is native for almost all Matrix
methods).
Upvotes: 0
Reputation: 214957
You can possibly try:
with(summary(M), j[i == 1])
# [1] 1 4
with(summary(M), j[i == 2])
# [1] 2
Wrap it as a function, assuming M
is a sparseMatrix
already:
f <- function(M, row) with(summary(M), j[i==row])
f(M, 1)
# [1] 1 4
f(M, 2)
# [1] 2
Upvotes: 1