Search
Question: rowsum or similar for RleArray?
0
gravatar for maltethodberg
10 weeks ago by
UCPH
maltethodberg40 wrote:

I'm experimenting with the new RleArrays from DelayedArray, and I was happy to see most operations work seamlessly just like a normal matrix. 

I often use the the very fast rowsum function to aggregate rows of matrix, but this does not seem to work on an RleArray. Is there an alternative to the rowsum-function in for RleArrays?

 

ADD COMMENTlink modified 10 weeks ago by Hervé Pagès ♦♦ 13k • written 10 weeks ago by maltethodberg40
0
gravatar for Aaron Lun
10 weeks ago by
Aaron Lun17k
Cambridge, United Kingdom
Aaron Lun17k wrote:

The closest I can think of is some work by Peter Hickey: https://github.com/PeteHaitch/DelayedMatrixStats. I don't know if this is applicable to RleArray objects, though.

ADD COMMENTlink written 10 weeks ago by Aaron Lun17k
0
gravatar for Hervé Pagès
10 weeks ago by
Hervé Pagès ♦♦ 13k
United States
Hervé Pagès ♦♦ 13k wrote:

Hi,

No rowsum method for DelayedMatrix objects yet, but there is a rowSums method. In the meantime, here is a quick and dirty implementation for rowsum that doesn't support reorder or na.rm (like rowsum.default and rowsum.data.frame do):

rowsum.DelayedMatrix <- function(x, group, reorder=TRUE, ...)
    apply(x, 2, function(col) sum(splitAsList(col, group)))

Then:

library(DelayedArray)
M <- as(matrix(runif(100000), nrow=2000), "RleArray")
group <- sample(8, 2000, replace=TRUE)
identical(rowsum(M, group), rowsum(as.matrix(M), group))
# [1] TRUE

Is not as fast as on an ordinary matrix though...

Cheers,

H.

ADD COMMENTlink modified 10 weeks ago • written 10 weeks ago by Hervé Pagès ♦♦ 13k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.2.0
Traffic: 165 users visited in the last hour