Find the value or position of the first match.

detect(.x, .p, ..., .right = FALSE)

detect_index(.x, .p, ..., .right = FALSE)



A list or atomic vector.


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 to TRUE will be modified.


Additional arguments passed on to .f.


If FALSE, the default, starts at the beginnging of the vector and move towards the end; if TRUE, starts at the end of the vector and moves towards the beginning.


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)
#> [1] 4
3:10 %>% detect_index(is_even)
#> [1] 2
3:10 %>% detect(is_even, .right = TRUE)
#> [1] 10
3:10 %>% detect_index(is_even, .right = TRUE)
#> [1] 8