I would like to automate the logical expression in statements like dplyr::filter or dplyr::case_when. For a reproducible example consider the mtcars and with a special column called id:
library(tidyverse)
dd <- mtcars %>% as_tibble() %>% mutate(id = letters[1:32])
The goal is to get the same result as with
identifier <- "d"
dd %>%
filter(id == identifier)
by using a function instead of the logical expression, like so
identifier <- "d"
is_id <- function(foo_id){
# body
}
dd %>%
filter(is_id(identifier))
The goal is to make a function is_id, which returns the logical expression used by filter. The function is_id <- function(car_id){ #body } should therefore evaluate the expression dd$id == foo_id and return the logical vector of its results. This should be achieved without repeating the .data argument (dd in this case) , e.g. not filter(dd, is_id(dd, "c")).
Is there a way to achieve this, and if so how?