detect {purrr} | R Documentation |
Find the value or position of the first match.
detect(.x, .p, ..., .right = FALSE) detect_index(.x, .p, ..., .right = FALSE)
.x |
A list or atomic 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 |
.right |
If |
detect
the value of the first item that matches the
predicate; detect_index
the position of the matching item.
If not found, detect
returns NULL
and detect_index
returns 0.
is_even <- function(x) x %% 2 == 0 3:10 %>% detect(is_even) 3:10 %>% detect_index(is_even) 3:10 %>% detect(is_even, .right = TRUE) 3:10 %>% detect_index(is_even, .right = TRUE)