Skip to content
Prev 304799 / 398513 Next

using apply with sparse matrix from package Matrix

> Hi:
    > I was trying to use apply on a sparse matrix from package Matrix,
    > and I get the error:

    > Error in asMethod(object) :
    > Cholmod error 'problem too large' at file ../Core/cholmod_dense.c, line 106

    > Is there a way to apply a function to all the rows without bumping
    > into this problem?

    > Here is a simplified example:

    >> dim(sm)
    > [1] 72913 43052

    >> class(sm)
    > [1] "dgCMatrix"
    > attr(,"package")
    > [1] "Matrix"

    >> str(sm)
    > Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
    > ..@ i       : int [1:6590004] 789 801 802 1231 1236 11739 17817
    > 17943 18148 18676 ...
    > ..@ p       : int [1:43053] 0 147 303 450 596 751 908 1053 1188 1347 ...
    > ..@ Dim     : int [1:2] 72913 43052
    > ..@ Dimnames:List of 2
    > .. ..$ : NULL
    > .. ..$ : NULL
    > ..@ x       : num [1:6590004] 0.601 0.527 0.562 0.641 0.684 ...
    > ..@ factors : list()

    >> my.sum<-apply(sm, 1, sum)
    > Error in asMethod(object) :
    > Cholmod error 'problem too large' at file ../Core/cholmod_dense.c, line 106

So, actually it would have worked (though not efficiently) if
your sm matrix would have been much smaller.

However,  we provide  rowSums(), rowMeans(), colSums(), colMeans() 
for all of our matrices, including the sparse ones.

So your present problem can be solved using

my.sum <- rowSums(sm)

Best regards,
Martin Maechler, ETH Zurich