**60**wrote:

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?

**13k**• written 9 months ago by maltethodberg •

**60**

Question: remove NA's from a DelayedArray

0

maltethodberg • **60** wrote:

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?

ADD COMMENT
• link
•
modified 9 months ago
by
Hervé Pagès ♦♦ **13k**
•
written
9 months ago by
maltethodberg • **60**

3

Hervé Pagès ♦♦ **13k** wrote:

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.

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.

1

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.

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: 260 users visited in the last hour