【发布时间】:2018-05-30 20:15:18
【问题描述】:
我正在寻找出现在搜索字符串中的关键字(在本例中为研究问题)。我想我已经接近了,但我不太确定我遇到了什么问题。我的数据框看起来像这样:
Q1 keywords
How do you assess strategic deterrence messaging? Deterrence messaging effects perception assessment
An energy transition for green growth Energy transition sustainable
Some other research question here research keywords topics etc
其中Q1 指的是问题,keywords 是单词列表(在这种情况下,清除了 AND、NOT 和 OR 的布尔搜索)。我要确定的是keywords 中的任何一个是否出现在Q1 字符串中,找到匹配项,并计算这种情况发生的频率(所以我可以说keywords 出现在column1 n% 的时间,在column2 n% 的时间...)。
这是我开始的地方,使用tidyverse:
df_final <- df %>%
mutate(matches = str_extract_all(
Q1,
str_c(df$keywords, collapse = "|") %>% regex(ignore_case = T)),
match = map_chr(matches, str_c, collapse = ", "),
count = map_int(matches, length)
)
但我没有得到任何匹配。我假设它可能与我的keyword 专栏有关?是否需要将其转换为向量或逗号分隔的列表才能正常工作?提前感谢您的建议!
编辑:dput() 的示例输出:
structure(list(Q1 = c("Assessing the effects of strategic deterrence messaging in the cognitive dimension",
"How do you assess effects of strategic deterrence messaging?",
"Determine Strategic Implications of Climate Change to USG/DoD"
), keywords = c("Deterrence messaging effects perception assessment",
"political philosophy sociology social sciences history marketing power structure government governing class bourgeoisie social class military class ruling class governing class",
"Climate Change Strategic Global Warming Strategic Climate Change Policy Global Warming Policy"
)), .Names = c("Q1", "keywords"), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
【问题讨论】:
-
你能用
dput()添加一个快速的df示例 -
完成 -- 在上面添加了一个编辑。