Learn R Programming

rray (version 0.1.0)

rray_if_else: Conditional selection

Description

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.

Usage

rray_if_else(condition, true, false)

Arguments

condition

A logical vector, matrix, array of rray.

true

A vector, matrix, array, or rray. This is the value in the result when condition is TRUE.

false

A vector, matrix, array, or rray. This is the value in the result when condition is FALSE.

Details

The dimension names of the output are taken as the common names of true and false.

Examples

Run this code
# NOT RUN {
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)

# }

Run the code above in your browser using DataLab