Learn R Programming

broadcast (version 0.1.7)

bc.b: Broadcasted Boolean Operations

Description

The bc.b() function performs broadcasted logical (or Boolean) operations on 2 arrays.

Please note that these operations will treat the input as logical.
Therefore, something like bc.b(1, 2, "==") returns TRUE, because both 1 and 2 are TRUE when treated as logical.

For regular relational operators, see bc.rel.

Usage

bc.b(x, y, op, ...)

# S4 method for ANY bc.b(x, y, op)

Value

Normally:

A logical array/vector as a result of the broadcasted Boolean operation.


If both x and y are type of raw:

A raw array/vector as a result of the broadcasted Boolean operation, where 01 codes for TRUE and 00 codes for FALSE.

This is convenient as raw requires less memory space than logical.


Arguments

x, y

conformable vectors/arrays of type logical, numeric, or raw.
Note that input with type of double will be coerced to integer.

op

a single string, giving the operator.
Supported Boolean operators: &, |, xor, nand, ==, !=, <, >, <=, >=.

...

further arguments passed to or from methods.

Details

bc.b() efficiently casts the input to logical.
Since the input is treated as logical, the following equalities hold for bc.b():

  • "==" is equivalent to (x & y) | (!x & !y), but faster;

  • "!=" is equivalent to xor(x, y);

  • "<" is equivalent to (!x & y), but faster;

  • ">" is equivalent to (x & !y), but faster;

  • "<=" is equivalent to (!x & y) | (y == x), but faster;

  • ">=" is equivalent to (x & !y) | (y == x), but faster.

See Also

broadcast_operators

Examples

Run this code
x.dim <- c(4:2)
x.len <- prod(x.dim)
x.data <- sample(c(TRUE, FALSE, NA), x.len, TRUE)
x <- array(x.data, x.dim)
y <- array(1:50, c(4,1,1))

bc.b(x, y, "&")
bc.b(x, y, "|")
bc.b(x, y, "xor")
bc.b(x, y, "nand")
bc.b(x, y, "==")
bc.b(x, y, "!=")

Run the code above in your browser using DataLab