keep {purrr} | R Documentation |
keep
and discard
are opposites. compact
is a handy
wrapper that removes all elements that are NULL
.
keep(.x, .p, ...) discard(.x, .p, ...) compact(.x, .p = identity)
.x |
A list or vector. |
.p |
A single predicate function, a formula describing such a
predicate function, or a logical vector of the same length as |
... |
Additional arguments passed on to |
These are usually called select
or filter
and reject
or
drop
, but those names are already taken. keep
is similar to
Filter()
but the argument order is more convenient, and the
evaluation of .f
is stricter.
rep(10, 10) %>% map(sample, 5) %>% keep(function(x) mean(x) > 6) # Or use a formula rep(10, 10) %>% map(sample, 5) %>% keep(~ mean(.x) > 6) # Using a string instead of a function will select all list elements # where that subelement is TRUE x <- rerun(5, a = rbernoulli(1), b = sample(10)) x x %>% keep("a") x %>% discard("a")