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 2704 7744
## [2,] -92 -92 -92
## [3,] 6084 6241 -69
## [4,] -72 -72 -72
## [5,] 4096 676 100
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 2916 3969 -29
## [2,] -92 -92 -92
## [3,] -69 6889 -69
## [4,] -72 -72 -72
## [5,] 3364 5476 256bc_ifelse
Broadcasted Ifelse
Description
The bc_ifelse() function 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.