remove NA's from a DelayedArray
1
0
Entering edit mode
maltethodberg ▴ 180
@maltethodberg-9690
Last seen 7 days ago
Denmark

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?

delayedarray • 1.6k views
ADD COMMENT
3
Entering edit mode
@herve-pages-1542
Last seen 4 hours ago
Seattle, WA, United States

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.

ADD COMMENT
0
Entering edit mode

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 as x, and (2) value is a scalar (i.e. atomic vector of length 1). That would cover x[is.na(x)] <- 0 but also things like x[x == 0.6] <- NA or x[x > 0 & log(x) < 0.5] <- -1 etc...

I'll try to get this in the DelayedArray package today or tomorrow.

H.

ADD REPLY
1
Entering edit mode

I added this to DelayedArray 0.3.20:

library(DelayedArray)
A <- DelayedArray(matrix(c(NA, 1:10, NA), ncol=3))
A[is.na(A)] <- 0
A
# DelayedMatrix object of 4 x 3 doubles:
#      [,1] [,2] [,3]
# [1,]    0    4    8
# [2,]    1    5    9
# [3,]    2    6   10
# [4,]    3    7    0

DelayedArray 0.3.20 should become available in BioC devel via biocLite() in the next 24 hours or so.

Cheers,

H.

ADD REPLY
0
Entering edit mode

cool thanks!

ADD REPLY

Login before adding your answer.

Traffic: 589 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6