rray_if_else()
is like ifelse()
, but works with matrices and arrays,
and fully supports broadcasting between the three inputs. Before the
operation is applied, condition
is cast to a logical, and true
and
false
are cast to their common type.
rray_if_else(condition, true, false)
condition | A logical vector, matrix, array of rray. |
---|---|
true | A vector, matrix, array, or rray. This is the value in the result
when |
false | A vector, matrix, array, or rray. This is the value in the
result when |
The dimension names of the output are taken as the common names of true
and false
.
cond <- c(TRUE, FALSE) true <- array( 1:2, dimnames = list(c("r1", "r2")) ) false <- rray( c(3, 4, 5, 6), dim = c(2, 2), dim_names = list(c("rr1", "rr2"), c("c1", "c2")) ) # - All inputs are broadcast to a common # shape of (2, 2). # - The first row of the output comes from # `true`, the second row comes from `false`. # - The names come from both `true` and `false`. rray_if_else(cond, true, false)#> <rray<dbl>[,2][2]> #> c1 c2 #> r1 1 1 #> r2 4 6