Create R sparse matrix from matrix CSV

Viewed 119

All, I have a large CSV file which contains an m x n matrix data, with row labels and column headers. I wish to generate a sparse matrix in R (e.g., dgCmatrix object) with this input file. We wish to avoid having to read the full matrix in memory within R and then casting it as a sparse matrix. I am wondering if there is any other straightforward way in R to do this?

I guess what is key is to generate the {row index, column index, value} representation from the original CSV. I came across this very helpful python script to first convert the CSV to a three column format but that would perhaps mean writing another file to disk and re-reading inside R. Alternatively, I see that writing C++ code to construct the sparse representation from the original CSV, and integrating it through Rcpp is an option, but perhaps there is a more straightforward solution?

Thanks!

0 Answers
Related