Question: rowsum or similar for RleArray?
gravatar for maltethodberg
8 months ago by
maltethodberg50 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 8 months ago by Hervé Pagès ♦♦ 13k • written 8 months ago by maltethodberg50
gravatar for Aaron Lun
8 months ago by
Aaron Lun19k
Cambridge, United Kingdom
Aaron Lun19k wrote:

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

ADD COMMENTlink written 8 months ago by Aaron Lun19k
gravatar for Hervé Pagès
8 months ago by
Hervé Pagès ♦♦ 13k
United States
Hervé Pagès ♦♦ 13k wrote:


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 do):

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


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...



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


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