Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a very large Sparse Matrix in R

I am trying to create a large sparse matrix, 10^5 by 10^5 in R, but am running into memory issues.

> Matrix(nrow=1e5,ncol=1e5,sparse=TRUE)
Error in Matrix(nrow = 1e+05, ncol = 1e+05, sparse = TRUE) : 
  too many elements specified

It looks like this is because the number of elements is larger than 2^31, which is the maximum integer value. But I am running this on a 64 bit machine.

> .Machine$integer.max
[1] 2147483647

Is there any way to create such a large, sparse matrix?

like image 271
aaronjg Avatar asked May 08 '13 16:05

aaronjg


1 Answers

It looks like the trick is to set data=0 rather than data=NA.

> Matrix(data=0,nrow=1e5,ncol=1e5,sparse=TRUE)
like image 127
aaronjg Avatar answered Sep 30 '22 10:09

aaronjg