I want to filter a data frame to include rows where the value of any column containing the string "bean" starts with "black" or contains "vanilla."
My code looks like this.
library(dplyr)
df2 <- df1 %>%
filter(
if_any(
.cols = contains('bean'),
grepl(
pattern = "^black*|*vanilla*",
ignore.case = T,
x = .
)
)
)
But that gives me this error:
Error: Problem with `filter()` input `..1`.
x Problem with `across()` input `.fns`.
ℹ Input `.fns` must be NULL, a function, a formula, or a list of functions/formulas.
ℹ Input `..1` is `if_all(...)`.
Run `rlang::last_error()` to see where the error occurred.
Running last_error() gives me this:
<error/dplyr_error>
Problem with `filter()` input `..1`.
x Problem with `across()` input `.fns`.
ℹ Input `.fns` must be NULL, a function, a formula, or a list of functions/formulas.
ℹ Input `..1` is `if_any(...)`.
Backtrace:
Run `rlang::last_trace()` to see the full context.
Running last_trace() gives me this:
<error/dplyr_error>
Problem with `filter()` input `..1`.
x Problem with `across()` input `.fns`.
ℹ Input `.fns` must be NULL, a function, a formula, or a list of functions/formulas.
ℹ Input `..1` is `if_any(...)`.
Backtrace:
█
1. ├─`%>%`(...)
2. ├─dplyr::filter(...)
3. ├─dplyr:::filter.data.frame(...)
4. │ └─dplyr:::filter_rows(.data, ...)
5. │ ├─base::withCallingHandlers(...)
6. │ └─mask$eval_all_filter(dots, env_filter)
7. ├─dplyr::if_any(...)
8. │ └─dplyr::across(...)
9. │ └─dplyr:::across_setup(...)
10. │ └─dplyr:::across_setup_impl(...)
11. │ └─rlang::abort(c("Problem with `across()` input `.fns`.", i = "Input `.fns` must be NULL, a function, a formula, or a list of functions/formulas."))
12. │ └─rlang:::signal_abort(cnd)
13. │ └─base::signalCondition(cnd)
14. └─(function (e) ...