Skip to contents

Element-wise selection based on a boolean predicate, mirroring R's ifelse(). For each element, returns the corresponding element from yes where test is TRUE and from no where test is FALSE.

prim_if() is the other conditional: it mirrors R's if construct and branches between two functions, evaluating only the selected one.

Usage

prim_ifelse(test, yes, no)

Arguments

test

(arrayish)
Predicate array. Must be a boolean or an R logical, and scalar or the same shape as yes.

yes, no

(arrayish)
Values to select from, of the same shape. Can be any data type. yes and no must have the same data type. An R value among them assumes the data type of the others when it is in its data type category, and its default data type when none of them has one.

Value

(arrayish)
Has the shape of yes and no, and the data type they agreed on.

Implemented Rules

  • stablehlo

  • quickr

  • reverse

StableHLO

Lowers to hlo_select(), specified under select.

Examples

# the result takes the branches' data type; `test` only selects
test <- nv_array(c(TRUE, FALSE, TRUE))
prim_ifelse(test, nv_array(c(1, 2, 3)), nv_array(c(4, 5, 6)))
#> AnvlArray
#>  1
#>  5
#>  3
#> [ CPUf32{3} ]