Search
Question: rowsum or similar for RleArray?
0
8 months ago by
UCPH
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?

modified 8 months ago by Hervé Pagès ♦♦ 13k • written 8 months ago by maltethodberg50
0
8 months ago by
Aaron Lun19k
Cambridge, United Kingdom
Aaron Lun19k 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.

0
8 months 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.