library("broadcast")
<- c(5, 3, 2)
x.dim <- prod(x.dim)
x.len
<- array(sample(1:100), x.dim)
x <- array(sample(1:100), c(5, 1, 1))
y
<- bc.i(x, y, ">")
cond
bc_ifelse(cond, yes = x^2, no = -y)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 3969 -51 -51
## [2,] 6561 2601 9025
## [3,] 5625 361 5776
## [4,] 7744 -75 -75
## [5,] -90 -90 -90
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] -51 6241 8836
## [2,] 1444 -24 4489
## [3,] 6084 289 2025
## [4,] 9409 8281 -75
## [5,] -90 -90 9604
bc_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 NA s.
|
yes , no
|
conformable 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
.
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]
isNA
whenyes
andno
are atomic, andout[i]
islist(NULL)
whenyes
andno
are recursive.