Selects elements from yes or no based on test, mirroring R's
ifelse().
nv_if() is the other conditional: it mirrors R's if construct and
branches between two functions, evaluating only the selected one.
Arguments
- test
(
arrayish)
Predicate array. Must be a boolean or an R logical, and scalar or the same shape as the non-scalar arguments.- yes, no
(
arrayish)
Values to return wheretestisTRUE/FALSE. Can be of any data type; the two are promoted to a common data type. Scalars (includingtest) are broadcast to the shape of the non-scalar arguments.
Value
(arrayish)
Has the common data type of yes and no, and their broadcast shape.
See also
prim_ifelse() for the underlying primitive, nv_if().
Examples
test <- nv_array(c(TRUE, FALSE, TRUE))
nv_ifelse(test, nv_array(c(1, 2, 3)), nv_array(c(4, 5, 6)))
#> AnvlArray
#> 1
#> 5
#> 3
#> [ CPUf32{3} ]
# scalar branches are broadcast and promoted to a common data type
nv_ifelse(test, nv_scalar(1L), nv_scalar(0.5))
#> AnvlArray
#> 1.0000
#> 0.5000
#> 1.0000
#> [ CPUf32{3} ]