Calimo
Calimo

Reputation: 7969

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.

Upvotes: 3

Views: 3483

Answers (2)

Good Fit
Good Fit

Reputation: 1316

@qoheleth's solution works for me. Just add more context about how to access elements of sparse matrix randomly.

N.B.: for sparse matrix created using Matrix package, big_sparse_mat@x attribute stores the indices of all non-zero elements for the matrix. So the randomly access indices should be within the right range, otherwise, you will get NA values.

Assume one wants to extract elements that are larger than 2 from the sparse matrix, the following code will do:

select_inds <- which( big_sparse_mat@x > 2.0)
select_elements <- big_sparse_mat@x[select_inds]

min_val <- min(select_elements)
max_val <- max(select_elements)

Upvotes: 2

qoheleth
qoheleth

Reputation: 2319

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).

Upvotes: 2

Related Questions