李...的确切匹配
通知
清除全部

从r中的文本列表中的单词列表中的确切匹配

RSS

(@anamika)
贵族成员
加入:2年前
帖子:1381
14/05/2021 12:25 PM

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

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

col_2 < - c(“工作说明更改”,

“技术NPI检查”,

“功能位置”,

“施工已经开始”,

“将会有constn coon”)

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

df $ col_2 < - tolower(DF $ col_2)

单词<-c(“ const”,“ constn”,“约束”,“ conduc”,

“构造”,“构造”,“构造”,“ consttntype”,“ constypes”,“ ct”,“ ct#”,“

“ CT2”

pattern_words < - 粘贴(单词,collapse =“ |”)

df $ result < - ifelse(str_detect(df $ col_2,regex(pattern_words)),“找到”,“找不到”)


引用
(@sathish)
成员 主持人
加入:2年前
帖子:1391
14/05/2021 12:27 PM

您可以简单地使用围绕单词的单词边界。

库(Stringr)

pattern_words < - paste0('\\ b',单词,'\\ b',collapse =“ |”)

df $ result <-c('找不到','找到')[str_detect(df $ col_2,pattern_words) + 1]

#or与`ifelse'

#df $ result < - ifelse(str_detect(df $ col_2,pattern_words),“找到”,“找不到”)

DF

#col_1 col_2结果

#1 1工作指导变更找不到

#2 2技术NPI检查找不到

#3 3找不到功能位置

#4 4建设已找到

#5 5将会发现constn coon

如果您愿意,也可以在此处使用GREPL将其保存在基本r中:

grepl(tatter_words,df $ col_2)


回复引用
分享:
Baidu