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,] 6724 9216 -64
#> [2,] 1089 3136 2025
#> [3,] 1600 -29 7056
#> [4,] -88 9604 -88
#> [5,] 1849 8464 3364
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 5776 5625 -64
#> [2,] 625 6241 2116
#> [3,] 4096 -29 8836
#> [4,] -88 -88 -88
#> [5,] -27 8100 1936bc_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]isyes[i]; -
when
test[i] == FALSE,out[i]isno[i]; -
when
test[i]isNA,out[i]isNAwhenyesandnoare atomic, andout[i]islist(NULL)whenyesandnoare recursive.