library("broadcast")
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, "&")
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] NA TRUE TRUE
#> [2,] TRUE TRUE NA
#> [3,] FALSE FALSE TRUE
#> [4,] NA FALSE TRUE
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] TRUE NA NA
#> [2,] FALSE FALSE NA
#> [3,] NA NA FALSE
#> [4,] FALSE FALSE NA
bc.b(x, y, "|")
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] TRUE TRUE TRUE
#> [2,] TRUE TRUE TRUE
#> [3,] TRUE TRUE TRUE
#> [4,] TRUE TRUE TRUE
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] TRUE TRUE TRUE
#> [2,] TRUE TRUE TRUE
#> [3,] TRUE TRUE TRUE
#> [4,] TRUE TRUE TRUE
bc.b(x, y, "xor")
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] NA FALSE FALSE
#> [2,] FALSE FALSE NA
#> [3,] TRUE TRUE FALSE
#> [4,] NA TRUE FALSE
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] FALSE NA NA
#> [2,] TRUE TRUE NA
#> [3,] NA NA TRUE
#> [4,] TRUE TRUE NA
bc.b(x, y, "nand")
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] NA FALSE FALSE
#> [2,] FALSE FALSE NA
#> [3,] TRUE TRUE FALSE
#> [4,] NA TRUE FALSE
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] FALSE NA NA
#> [2,] TRUE TRUE NA
#> [3,] NA NA TRUE
#> [4,] TRUE TRUE NA
bc.b(x, y, "==")
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] NA TRUE TRUE
#> [2,] TRUE TRUE NA
#> [3,] FALSE FALSE TRUE
#> [4,] NA FALSE TRUE
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] TRUE NA NA
#> [2,] FALSE FALSE NA
#> [3,] NA NA FALSE
#> [4,] FALSE FALSE NA
bc.b(x, y, "!=")
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] NA FALSE FALSE
#> [2,] FALSE FALSE NA
#> [3,] TRUE TRUE FALSE
#> [4,] NA TRUE FALSE
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] FALSE NA NA
#> [2,] TRUE TRUE NA
#> [3,] NA NA TRUE
#> [4,] TRUE TRUE NAbc.b
Broadcasted Boolean Operations
Description
The bc.b() method 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 signature 'ANY'
bc.b(x, y, op)
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, nor, ==, !=, <, >, <=, >=. "nand" is defined here as !(x & y), and "nor" is defined here as !(x | y).
|
β¦
|
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
!xor(x, y)(i.e. bi-conditional operation), but faster; -
"!=" is equivalent to
xor(x, y); -
"<" is equivalent to
(!x & y)(i.e. only y), but faster; -
">" is equivalent to
(x & !y)(i.e. only x), but faster; -
"<=" is equivalent to
(!x & y) | (y == x)(i.e. material implication), but faster; -
">=" is equivalent to
(x & !y) | (y == x)(i.e. converse implication), but faster.
Note that the & and | operators handle NAs slightly differently than the relational operators.
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.