Question: rowsum or similar for RleArray?
gravatar for maltethodberg
6 months ago by
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 6 months ago by Hervé Pagès ♦♦ 13k • written 6 months ago by maltethodberg40
gravatar for Aaron Lun
6 months ago by
Aaron Lun18k
Cambridge, United Kingdom
Aaron Lun18k 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 6 months ago by Aaron Lun18k
gravatar for Hervé Pagès
6 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 6 months ago • written 6 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: 322 users visited in the last hour