Search code examples
r32bit-64bitsparse-matrixrcpp

How to get a big sparse matrix in R? (> 2^31-1)


I use some C++ code to take a text file from a database and create a dgcMatrix type sparse matrix from the Matrix package. For the first time, I'm trying to build a matrix that has more than 2^31-1 non-sparse members, which means that the index vector in the sparse matrix object must also be longer than that limit. Unfortunately, vectors seem to use 32-bit integer indices, as do NumericVectors in Rcpp.

Short of writing an entire new data type from the ground up, does R provide any facility for this? I don't think I can use too exotic a solution as I need glmnet to recognize the resultant object.


Solution

  • In recent versions of R, vectors are indexed by the R_xlen_t type, which is 64 bits on 64 bits platforms and just int on 32 bit platforms.

    Rcpp so far still uses int everywhere. I would encourage you to request the feature on their issue list. It is not hard, but needs systematic involvement of someone with skills, time and willingness. The development version of Rcpp11 uses the correct type, perhaps they can use that as a model.

    Note however that even though R uses 64 bit unsigned integers on 64 bit plaforms, you are in fact limited to the range that can be handled by the double type, which is what R will give you if you ask for the length of a vector. R has no 64 bit integer type that it can represent natively, so when you ask for the length of a vector you either get an int or a double depending on the value.