【发布时间】:2021-07-06 01:03:19
【问题描述】:
我正在尝试对文本数据库执行以下搜索。
这里是示例数据库,df
df <- data.frame(
id = c(1, 2, 3, 4, 5, 6),
name = c("john doe", "carol jones", "jimmy smith",
"jenny ruiz", "joey jones", "tim brown"),
place = c("reno nevada", "poland maine", "warsaw poland",
"trenton new jersey", "brooklyn new york", "atlanta georgia")
)
我有一个字符串向量,其中包含我要查找的术语。
new_search <- c("poland", "jones")
我将向量传递给 str_detect 以在 df 中的任何列中查找 new_search 中的任何字符串,然后返回匹配的行...
df %>%
filter_all(any_vars(str_detect(., paste(new_search, collapse = "|"))))
问题...如何将 str_detect 的结果提取到新列中?
对于返回的每一行......我想生成一个成功匹配的术语列表,并将它们放在一个列表或字符向量(matched_terms)中......像这样......
id name place matched_terms
1 2 carol jones poland maine c("jones", "poland")
2 3 jimmy smith warsaw poland c("poland")
3 5 joey jones brooklyn new york c("jones")
【问题讨论】: