[R] Scaling rows of a large Matrix::sparseMatrix()

tomdharray at gmail.com tomdharray at gmail.com
Thu Jan 14 02:11:28 CET 2016


Hello Gerrit,

Thanks. Your proposal works in general, but I get memory allocation
errors with my actual 57,000 x 14,000 matrix.

The fix which I now use is to scale the data before I build the matrix;
see below.


Cheers,

Dirk


## Code Start -----------------------------

library(parallel)

rowscale <- function(.x) cbind(.x[,1:3], .x[,3] / sum(.x[,3]))

y <- split(x = x, f = x[,1])


localSocketCluster <- parallel::makeCluster(spec = 4, type = "SOCK")

y <- parallel::parLapply(cl = localSocketCluster, X = y, fun = rowscale)

parallel::stopCluster(cl = localSocketCluster)

x <- do.call(what = rbind, args = y)

## build the sparse matrix
M <- Matrix::sparseMatrix(dims = c(N_ROW, N_COL),
       i = x[,1], j = x[,2], x = x[,4])


## Code End -----------------------------

On 16-01-13 03:23 AM, Gerrit Eichner wrote:
> Hello, Dirk,
> 
> maybe I'm missing something, but to avoid your for-loop-approach doesn't
> 
> M <- M/Matrix::rowSums(M)
> 
> do what you want?
> 
>  Hth  --  Gerrit
> 
> ---------------------------------------------------------------------
> Dr. Gerrit Eichner                   Mathematical Institute, Room 212
> gerrit.eichner at math.uni-giessen.de   Justus-Liebig-University Giessen
> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
> Fax: +49-(0)641-99-32109            http://www.uni-giessen.de/eichner
> ---------------------------------------------------------------------
>



More information about the R-help mailing list