【问题标题】:r grepl search for multiple patterns in a variable [duplicate]r grepl 在变量中搜索多个模式[重复]
【发布时间】:2015-02-05 17:52:39
【问题描述】:

我正在尝试搜索字符串变量,每次找到确定的模式时,搜索功能都会告诉我 TRUE。我正在使用 grepl 来查找匹配项:

grepl(pattern,x)

模式需要从几个单词构建,而这些单词又是从 csv 文件中捕获的。

我想我在构建模式时做错了,但我找不到错误。

下面有一个虚构的例子

#example file with the string data to classify
des<-c("DDD SS","FFFFF P","AAA EKO BBB","KK SUPER OO","JJ")
num<-c(5,6,2,7,9)
d0<-data.frame(des,num)


#example file with the pattern to search for as rows
t0<-data.frame(c("SUPER","A ISABEL","EKO"))

t1<-as.list(t(t0)) #traspose the vector as la list
t2<-do.call("paste",c(t1,sep="'|'")) #collapse to a single string with '|' (or) symbol for the grepl pattern

cl<-grepl(t2,d0$des)

最后的 grepl 没有找到任何匹配项

> cl
[1] FALSE FALSE FALSE FALSE FALSE

有什么建议吗?

提前致谢

【问题讨论】:

    标签: regex r pattern-matching grepl


    【解决方案1】:

    试试

     t2 <- paste(t1, collapse="|")
     grepl(t2, d0$des)
    #[1] FALSE FALSE  TRUE  TRUE FALSE
    

    【讨论】:

      猜你喜欢
      • 2014-12-01
      • 1970-01-01
      • 2021-10-23
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多