rowsum or similar for RleArray?
2
0
Entering edit mode
maltethodberg ▴ 140
@maltethodberg-9690
Last seen 4 weeks ago
Denmark

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?

delayedarray rowsum • 735 views
0
Entering edit mode
Aaron Lun ★ 27k
@alun
Last seen 2 hours ago
The city by the bay

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
Entering edit mode
@herve-pages-1542
Last seen 7 hours ago
Seattle, WA, United States

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.