| parallel-operators | R Documentation |
any() and all()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().
vec_pany(
...,
.missing = NA,
.size = NULL,
.arg = "",
.error_call = current_env()
)
vec_pall(
...,
.missing = NA,
.size = NULL,
.arg = "",
.error_call = current_env()
)
... |
Logical vectors of equal size. |
.missing |
Value to use when a missing value is encountered. One of:
|
.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. |
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.
A logical vector the same size as the vectors in ....
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()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.