detect | R Documentation |
Find the value or position of the first match
detect( .x, .f, ..., .dir = c("forward", "backward"), .right = NULL, .default = NULL ) detect_index(.x, .f, ..., .dir = c("forward", "backward"), .right = NULL)
.x |
A list or vector. |
.f |
A function, specified in one of the following ways:
|
... |
Additional arguments passed on to |
.dir |
If |
.right |
|
.default |
The value returned when nothing is detected. |
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.
keep()
for keeping all matching values.
is_even <- function(x) x %% 2 == 0 3:10 |> detect(is_even) 3:10 |> detect_index(is_even) 3:10 |> detect(is_even, .dir = "backward") 3:10 |> detect_index(is_even, .dir = "backward") # Since `.f` is passed to as_mapper(), you can supply a # lambda-formula or a pluck object: x <- list( list(1, foo = FALSE), list(2, foo = TRUE), list(3, foo = TRUE) ) detect(x, "foo") detect_index(x, "foo") # If you need to find all values, use keep(): keep(x, "foo") # If you need to find all positions, use map_lgl(): which(map_lgl(x, "foo"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.