基本上,我有一个单词列表,我在寻找出现在文本中的单词。期望的结果是,在搜索模式时总是找到最后一列。我正在寻找一个确切的匹配,是目前在文字。我不想要这些组合。对于前三条记录,应该找不到。
col_1 < - c(1、2、3、4、5)
Col_2 <- c(“作业指导书变更”,
“技术npi检查”,
“功能位置”,
“建设已开始”,
“会有康斯坦coon”)
df < - as.data.frame (cbind (col_1 col_2))
df col_2 < -低(df col_2美元)
< - c(“常量”,“constn”,“建设”、“构造”,
“构造”、“建设”、“构建”、“consttntype”,“constypes”、“ct”、“ct #”,
“ct2”
)
pattern n_words <-粘贴(words, collapse = "|")
df$result<- ifelse(str_detect(df$col_2, regex(pattern_words)),"Found","Not Found")
你可以简单地在单词周围使用边界这个词。
库(stringr)
pattern_words < - paste0(“\ \ b”,话说,“\ \ b”,崩溃=“|”)
df$result <- c('Not Found', 'Found')[str_detect(df$col_2, pattern_words) + 1]
#或“ifelse”
#df$result <- ifelse(str_detect(df$col_2, pattern_words), "Found", "Not Found")
df
# col_1 col_2 result
没有发现工作指导书的变更
未发现技术新产品导入检查
没有找到3个功能性的位置
4个建筑已经开始建造了
康斯坦·库恩被发现了
如果你想,你也可以在这里使用grepl来保持base R:
grepl (pattern_words, df col_2美元)