Entering edit mode
Given that subsetting works slightly different for a DelayedArray compared to a matrix, is there a good alternative to m[is.na(m)] <- 0
for replacing NA's in a DelayedArray?
Given that subsetting works slightly different for a DelayedArray compared to a matrix, is there a good alternative to m[is.na(m)] <- 0
for replacing NA's in a DelayedArray?
Hi,
I need to think of a more natural/intuitive way to support this but in the mean time you can use the following:
replaceNAs <- function(x, value) { stopifnot(is(x, "DelayedArray"), is.vector(value), is.atomic(value), length(value) == 1L) DelayedArray:::register_delayed_op(x, function(xx) ifelse(is.na(xx), value, xx) ) }
Then:
library(DelayedArray) A <- DelayedArray(matrix(c(NA, 1:10, NA), ncol=3)) A # DelayedMatrix object of 4 x 3 integers: # [,1] [,2] [,3] # [1,] NA 4 8 # [2,] 1 5 9 # [3,] 2 6 10 # [4,] 3 7 NA replaceNAs(A, 0) # DelayedMatrix object of 4 x 3 logicals: # [,1] [,2] [,3] # [1,] 0 4 8 # [2,] 1 5 9 # [3,] 2 6 10 # [4,] 3 7 0
Cheers,
H.
Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
After more thoughts on this, I think we can support delayed subassignment (i.e.
x[i] <- value
) at least in the special case where: (1)i
is a logical DelayedArray with the same dimensions asx,
and (2)value
is a scalar (i.e. atomic vector of length 1). That would coverx[is.na(x)] <- 0
but also things likex[x == 0.6] <- NA
orx[x > 0 & log(x) < 0.5] <- -1
etc...I'll try to get this in the DelayedArray package today or tomorrow.
H.
I added this to DelayedArray 0.3.20:
DelayedArray 0.3.20 should become available in BioC devel via
biocLite()
in the next 24 hours or so.Cheers,
H.
cool thanks!