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))的方法来找出列A1中包含单词“ search”的单元格。

在我看来,这grep可能是我正在寻找的功能,但我不知道如何正确使用它。

4

1 回答 1

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?"
于 2013-01-15T23:27:19.327 回答