使用多模式字符向量的grep

使用多模式字符向量的grep

我试着用grep测试字符串向量是否存在于另一个向量中,并输出存在的值(匹配模式)。

我有这样一个数据框架:

FirstName Letter   
Alex      A1
Alex      A6
Alex      A7
Bob       A1
Chris     A9
Chris     A6

我在“信函”列中找到字符串模式的向量,例如:c("A1", "A9", "A6").

我想检查模式向量中的任何字符串是否存在于“信函”列中。如果是的话,我想要的是唯一值的输出。

问题是,我不知道怎么用grep有多种模式。我试过:

matches <- unique (
    grep("A1| A9 | A6", myfile$Letter, value=TRUE, fixed=TRUE))

但是它给了我0场比赛,这不是真的,有什么建议吗?


侃侃无极
浏览 475回答 3
3回答

MYYA

除了@Marek关于不包括fixed==TRUE,您还需要在正则表达式中不包含空格。应该是"A1|A9|A6".你还提到有很多模式。假设它们在向量中toMatch&nbsp;<-&nbsp;c("A1",&nbsp;"A9",&nbsp;"A6")然后,您可以直接从这里创建正则表达式。matches&nbsp;<-&nbsp;unique&nbsp;(grep(paste(toMatch,collapse="|"),&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myfile$Letter,&nbsp;value=TRUE))

holdtom

好的答案,但是不要忘记filter()来自Dplyr:patterns <- c("A1", "A9", "A6")>your_df&nbsp; FirstName Letter1&nbsp; &nbsp; &nbsp; Alex&nbsp; &nbsp; &nbsp;A12&nbsp; &nbsp; &nbsp; Alex&nbsp; &nbsp; &nbsp;A63&nbsp; &nbsp; &nbsp; Alex&nbsp; &nbsp; &nbsp;A74&nbsp; &nbsp; &nbsp; &nbsp;Bob&nbsp; &nbsp; &nbsp;A15&nbsp; &nbsp; &nbsp;Chris&nbsp; &nbsp; &nbsp;A96&nbsp; &nbsp; &nbsp;Chris&nbsp; &nbsp; &nbsp;A6result <- filter(your_df, grepl(paste(patterns, collapse="|"), Letter))>result&nbsp; FirstName Letter1&nbsp; &nbsp; &nbsp; Alex&nbsp; &nbsp; &nbsp;A12&nbsp; &nbsp; &nbsp; Alex&nbsp; &nbsp; &nbsp;A63&nbsp; &nbsp; &nbsp; &nbsp;Bob&nbsp; &nbsp; &nbsp;A14&nbsp; &nbsp; &nbsp;Chris&nbsp; &nbsp; &nbsp;A95&nbsp; &nbsp; &nbsp;Chris&nbsp; &nbsp; &nbsp;A6

小怪兽爱吃肉

根据Brian Digg的文章,以下是筛选列表的两个有用函数:#Returns&nbsp;all&nbsp;items&nbsp;in&nbsp;a&nbsp;list&nbsp;that&nbsp;are&nbsp;not&nbsp;contained&nbsp;in&nbsp;toMatch#toMatch&nbsp;can&nbsp;be&nbsp;a&nbsp;single&nbsp;item&nbsp;or&nbsp;a&nbsp;list&nbsp;of&nbsp;itemsexclude &nbsp;<-&nbsp;function&nbsp;(theList,&nbsp;toMatch){ &nbsp;&nbsp;return(setdiff(theList,include(theList,toMatch)))}#Returns&nbsp;all&nbsp;items&nbsp;in&nbsp;a&nbsp;list&nbsp;that&nbsp;ARE&nbsp;contained&nbsp;in&nbsp;toMatch#toMatch&nbsp;can&nbsp;be &nbsp;&nbsp;&nbsp;a&nbsp;single&nbsp;item&nbsp;or&nbsp;a&nbsp;list&nbsp;of&nbsp;itemsinclude&nbsp;<-&nbsp;function&nbsp;(theList,&nbsp;toMatch){ &nbsp;&nbsp;matches&nbsp;<-&nbsp;unique&nbsp;(grep(paste(toMatch,collapse="|"),&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theList,&nbsp;value=TRUE)) &nbsp;&nbsp;return(matches)}
打开App,查看更多内容
随时随地看视频慕课网APP