Search code examples
rsparse-matrix

How to access a few elements of a sparse matrix from R Matrix library?


Let's say I have a big sparse matrix:

library(Matrix)
nrow <- 223045
ncol <- 9698
big <- Matrix(0, nrow, ncol, sparse = TRUE)
big[1, 1] <- 1

Now I want to access the first element:

big[1]
Error in asMethod(object) : 
  Cholmod error 'problem too large' at file ../Core/cholmod_dense.c, line 105

For some reason it tries to convert my matrix to a dense matrix. In fact, looks like the method is inherited from Matrix rather than from a sparse class:

showMethods("[")
[...]
x="dgCMatrix", i="numeric", j="missing", drop="missing"
    (inherited from: x="Matrix", i="index", j="missing", drop="missing")
[...]

Of course I could use the full [i, j] indexing

big[1, 1]

but I want to access a few random elements throughout the matrix, like

random.idx <- c(1880445160,  660026771, 1425388501,  400708750, 2026594194, 1911948714)
big[ random.idx ]

and those can't be accessed with the [i, j] notation (or you'd need to go element-wise, not really efficient).

How can I access random elements of this matrix without converting it to a dense matrix? Alternative solutions (other packages, et) are welcome.


Solution

  • You can extract the elements of the Matrix directly using the S4 extraction @ without converting it to an ordinary matrix first. For example,

    big@x[1]
    big@x[random.idx]
    

    In fact, you can extract other attributes as well. See str(big).