Learn R Programming

vctrs (version 0.7.0)

parallel-operators: Parallel any() and all()

Description

These functions are variants of any() and all() that work in parallel on multiple inputs at once. They work similarly to how pmin() and pmax() are parallel variants of min() and max().

Usage

vec_pany(
  ...,
  .missing = NA,
  .size = NULL,
  .arg = "",
  .error_call = current_env()
)

vec_pall( ..., .missing = NA, .size = NULL, .arg = "", .error_call = current_env() )

Value

A logical vector the same size as the vectors in ....

Arguments

...

Logical vectors of equal size.

.missing

Value to use when a missing value is encountered. One of:

  • NA to propagate missing values. With this, missings are treated the same way as | or &.

  • FALSE to treat missing values as FALSE.

  • TRUE to treat missing values as TRUE.

.size

An optional output size. Only useful to specify if it is possible for no inputs to be provided.

.arg

Argument name used in error messages.

.error_call

The execution environment of a currently running function, e.g. caller_env(). The function will be mentioned in error messages as the source of the error. See the call argument of abort() for more information.

Details

vec_pany() and vec_pall() are consistent with any() and all() when there are no inputs to process in parallel:

  • any() returns FALSE with no inputs. Similarly, vec_pany(.size = 1) returns FALSE.

  • all() returns TRUE with no inputs. Similarly, vec_pall(.size = 1) returns TRUE.

Examples

Run this code
a <- c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, NA, NA, NA)
b <- c(TRUE, FALSE, NA, TRUE, FALSE, NA, TRUE, FALSE, NA)

# Default behavior treats missings like `|` does
vec_pany(a, b)
a | b

# Default behavior treats missings like `&` does
vec_pall(a, b)
a & b

# Remove missings from the computation, like `na_rm = TRUE`
vec_pany(a, b, .missing = FALSE)
(a & !is.na(a)) | (b & !is.na(b))

vec_pall(a, b, .missing = TRUE)
(a | is.na(a)) & (b | is.na(b))

# `vec_pall()` can be used to implement a `dplyr::filter()` style API
df <- data_frame(id = seq_along(a), a = a, b = b)

keep_rows <- function(x, ...) {
  vec_slice(x, vec_pall(..., .missing = FALSE))
}
drop_rows <- function(x, ...) {
  vec_slice(x, !vec_pall(..., .missing = FALSE))
}

# "Keep / Drop the rows when both a and b are TRUE"
# These form complements of one another, even with `NA`s.
keep_rows(df, a, b)
drop_rows(df, a, b)

# Same empty behavior as `any()` and `all()`
vec_pany(.size = 1)
any()

vec_pall(.size = 1)
all()

Run the code above in your browser using DataLab