2015-02-05 38 views
0

我試圖搜索到一個字符串變量,每次發現確定模式時,搜索功能告訴我爲TRUE。我使用grepl找到匹配:r 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 

什麼建議嗎?

在此先感謝

回答

1

嘗試

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

非常感謝!它訣竅! – 2015-02-05 18:18:25