mardi 28 mai 2019

Generating random sparse matrix R

I am dealing with large matrices - on the order of 10^8 columns and 10^3-10^4 rows. Since these matrices are only ones and zeros, I think the sparse construction in the Matrix package is appropriate. However, I don't see a way to generate a random matrix like in the example below. Note that non-zero entries are defined by the column probabilities col_prob.

set.seed(1)#For reproducibility
ncols <- 20
nrows <- 10
col_prob <- runif(ncols,0.1,0.2)
rmat <- matrix(rbinom(nrows*ncols,1,col_prob),
       ncol=ncols,byrow=T)

Certainly I can convert rmat into a sparse matrix:

rmat_sparse <- Matrix(rmat,sparse=T)

However, I would like to generate the sparse matrix in one step. I'm not sure that the function Matrix::rsparsematrix can accomplish this.

Any thoughts?




Aucun commentaire:

Enregistrer un commentaire