如何对包含给定单词的句子的向量进行子集化

时间:2013-01-15 23:22:23

标签: regex string r subset

假设我有一个句子矢量:

Vector
Juan is searching for a magazine.
Julia searched her car.
Go to the market to buy eggs.
Your name is unsearchable.
Search for me when you get to Paris.
Can you search for a low cost solution?

我想要这个向量的一个子集,它只包含带有“搜索”或其变体的词条(即搜索,不可搜索,搜索)。在excel中,我可能会使用类似ISNUMBER(SEARCH("search",A1))的内容来查找A1列中包含“search”字样的单元格。

在我看来,grep可能是我正在寻找的功能,但我无法弄清楚如何正确使用它。

1 个答案:

答案 0 :(得分:5)

如果vector是您指定的垂直位置:

> grep("search", vector, ignore.case=TRUE)
[1] 1 2 4 5 6
> vector[grep("search", vector, ignore.case=TRUE)]
[1] "Juan is searching for a magazine."      
[2] "Julia searched her car."                
[3] "Your name is unsearchable."             
[4] "Search for me when you get to Paris."   
[5] "Can you search for a low cost solution?"