Do every, some, or none of the elements of a list satisfy a predicate?
Source:R/every-some-none.R
every.Rd
some()
returnsTRUE
when.p
isTRUE
for at least one element.every()
returnsTRUE
when.p
isTRUE
for all elements.none()
returnsTRUE
when.p
isFALSE
for all elements.
Arguments
- .x
A list or vector.
- .p
A predicate function (i.e. a function that returns either
TRUE
orFALSE
) specified in one of the following ways:A named function, e.g.
is.character
.An anonymous function, e.g.
\(x) all(x < 0)
orfunction(x) all(x < 0)
.A formula, e.g.
~ all(.x < 0)
. You must use.x
to refer to the first argument). Only recommended if you require backward compatibility with older versions of R.
- ...
Additional arguments passed on to
.p
.
Examples
x <- list(0:10, 5.5)
x |> every(is.numeric)
#> [1] TRUE
x |> every(is.integer)
#> [1] FALSE
x |> some(is.integer)
#> [1] TRUE
x |> none(is.character)
#> [1] TRUE
# Missing values are propagated:
some(list(NA, FALSE), identity)
#> [1] NA
# If you need to use these functions in a context where missing values are
# unsafe (e.g. in `if ()` conditions), make sure to use safe predicates:
if (some(list(NA, FALSE), rlang::is_true)) "foo" else "bar"
#> [1] "bar"