bc_ifelse

Broadcasted Ifelse

Description

The bc_ifelse() method performs a broadcasted form of ifelse.

Usage

bc_ifelse(test, yes, no, ...)

## S4 method for signature 'ANY'
bc_ifelse(test, yes, no)

Arguments

test a vector or array, with the type logical, integer, or raw, and a length equal to prod(bc_dim(yes, no)).
If yes / no are of type raw, test is not allowed to contain any NAs.
yes, no conformable vectors/arrays of the same type.
All atomic types are supported.
Recursive arrays of type list are also supported.
… further arguments passed to or from methods.

Value

The output, here referred to as out, will be an array of the same type as yes and no.
If test has the same dimensions as bc_dim(yes, no), then out will also have the same dimnames as test.
If test is a broadcaster, then out will also be a broadcaster.

After broadcasting yes against no, given any element index i, the following will hold for the output:

  • when test[i] == TRUE, out[i] is yes[i];

  • when test[i] == FALSE, out[i] is no[i];

  • when test[i] is NA, out[i] is NA when yes and no are atomic, and out[i] is list(NULL) when yes and no are recursive.

Examples

library("broadcast")


x.dim <- c(5, 3, 2)
x.len <- prod(x.dim)

x <- array(sample(1:100), x.dim)
y <- array(sample(1:100), c(5, 1, 1))

cond <- bc.i(x, y, ">")

bc_ifelse(cond, yes = x^2, no = -y)
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,] 8100 2500 4356
#> [2,]  -81 8836  -81
#> [3,]  529 1156  441
#> [4,]  -31 5184 6241
#> [5,] 3844  -38  -38
#> 
#> , , 2
#> 
#>      [,1] [,2]  [,3]
#> [1,]  324 5476  5929
#> [2,]  -81  -81   -81
#> [3,] 2025 3481  2601
#> [4,] 1600 7921 10000
#> [5,]  -38 3364  9409