2013-01-15 73 views
2

比方說,我有句子的向量:如何子集包含給定詞的句子的矢量

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))的東西來找出包含單詞「search」的列A1中的哪些單元格。

在我看來,grep可能是我正在尋找的功能,但我無法弄清楚如何正確使用它。

+2

您需要'值= TRUE'參數的grep。 'grep('search',Vector,value = TRUE)' – Justin

回答

5

如果vector是您指定的vertor:

> 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?"