通知
清除所有

完全匹配的单词列表,从一个文本在R

RSS

(@anamika)
贵族成员
加入:1年前
帖子:1381
14/05/2021需要点

基本上,我有一个单词列表,我在文本中寻找单词。期望的结果是在搜索模式时总是找到最后一列。我正在寻找一个确切的匹配,是目前的文字。我不想要组合。对于前三个记录,应该找不到它。

col_1 < - c(1、2、3、4、5)

Col_2 <- c(“工作指令更改”,

“技术npi检查”,

“功能位置”,

“建设已开始”,

“将会有康斯坦浣熊”)

df < - as.data.frame (cbind (col_1 col_2))

df col_2 < -低(df col_2美元)

< - c(“常量”,“constn”,“建设”、“构造”,

“构造”、“建设”、“构建”、“consttntype”,“constypes”、“ct”、“ct #”,

“ct2”

Pattern_words <-粘贴(words, collapse = "|")

df$result<- if (str_detect(df$col_2, regex(pattern_words)),"Found","Not Found")


报价
(@sathish)
成员 主持人
加入:1年前
帖子:1391
14/05/2021 27点

您可以简单地在单词周围使用单词边界。

库(stringr)

pattern_words < - paste0(“\ \ b”,话说,“\ \ b”,崩溃=“|”)

df$result <- c('Not Found', 'Found')[str_detect(df$col_2, pattern_words) + 1]

#或“ifelse”

#df$result <- if (str_detect(df$col_2, pattern_words), "Found", "Not Found")

df

# col_1 col_2 result

#1 1工作指令更改未找到

未发现技术新产品导入检查

#3 3个功能位置没有找到

#4 4建设已经开始发现

将会有康斯坦浣熊发现

如果你想,你也可以在这里使用grepl来保持它在基R:

grepl (pattern_words, df col_2美元)


回复报价
分享:
Baidu