[R] Fast way to populate a sparse matrix
stefanML at collocations.de
Fri Apr 25 09:09:31 CEST 2014
On 24 Apr 2014, at 23:56, Greg Snow <538280 at gmail.com> wrote:
> adjM <- Matrix(0,nrow=10,ncol=10)
> locs <- cbind( sample(1:10), sample(1:10) )
> vals <- rnorm(10)
> adjM[ locs ] <- vals
... and once you've got your data in this format, why not construct the sparse matrix directly?
adjM <- sparseMatrix(i = locs[,1], j = locs[,2], x = vals)
I've found this to be very efficient and have used it with sparse matrices containing up to around 100 million nonzero entries.
Hope this helps,
More information about the R-help