detect
Find the value or position of the first match.
Find the value or position of the first match.
Usage
detect(.x, .p, ..., .right = FALSE)detect_index(.x, .p, ..., .right = FALSE)
Arguments
- .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
.x
. Alternatively, if the elements of.x
are themselves lists of objects, a string indicating the name of a logical element in the inner lists. Only those elements where.p
evaluates toTRUE
will be modified.- ...
Additional arguments passed on to
.f
.- .right
If
FALSE
, the default, starts at the beginnging of the vector and move towards the end; ifTRUE
, starts at the end of the vector and moves towards the beginning.
Value
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.
Examples
# NOT RUN {
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)
# }