【发布时间】:2021-07-28 06:42:34
【问题描述】:
我希望将我的函数迁移到新创建的 across。
我使用filter_at 在多个列中搜索多个关键字的函数。
但是,我正在努力使用across 复制它,如下所示:
library(tidyverse)
raw_df <- tibble::tribble(
~cust_name, ~other_desc, ~trans, ~val,
"Cisco", "nothing", "a", 100L,
"bad_cs", "cisCo", "s", 101L,
"Ibm", "nothing", "d", 102L,
"bad_ib", "ibM", "f", 102L,
"oraCle", "Oracle", "g", 103L,
"mSft", "nothing", "k", 103L,
"noth", "Msft", "j", 104L,
"noth", "oracle", "l", 104L
)
search_string = c("ibm", "cisco")
# Done using `filter_at`
raw_df %>%
filter_at(.vars = vars(cust_name, other_desc),
.vars_predicate = any_vars(str_detect(., regex(paste(search_string, collapse = "|"), ignore_case = TRUE)))
) %>% unique()
# Not able to replicate result with `across`
raw_df %>%
filter(across(
.cols = c(cust_name, other_desc),
.fns = ~str_detect(.), regex(paste(search_string, collapse = "|"), ignore_case = TRUE)))
raw_df %>%
filter(str_detect,
across(any_of(cust_name, other_desc),
regex(paste(search_string, collapse = "|"), ignore_case = TRUE)))
【问题讨论】: