在R中跨单个向量删除多个字符串中的重复字

在R中跨单个向量删除多个字符串中的重复字,r,text,nlp,duplicates,R,Text,Nlp,Duplicates,我有一个由多个字符串组成的向量(在R中): 我已尝试以下解决方案:。但是,这只是将我的多个字符串合并成一个复杂字符串。我们可以使用gsub匹配两组单词和一个单词重复 gsub("((\\w+\\s+\\w+\\s?)|(\\w+\\s+))\\1+", "\\1", vec) #[1] "the cat ran up the tree" #[2] "the dog ran up the tree"

我有一个由多个字符串组成的向量(在R中):


我已尝试以下解决方案:。但是,这只是将我的多个字符串合并成一个复杂字符串。

我们可以使用
gsub
匹配两组单词和一个单词重复

gsub("((\\w+\\s+\\w+\\s?)|(\\w+\\s+))\\1+", "\\1", vec)
#[1] "the cat ran up the tree"    
#[2]  "the dog ran up the tree"     
#[3] "the squirrel ran up the tree"

你能推荐一个涵盖这些规则的正则表达式教程吗?@Englishman Bob可能会给出一些类似的语法解释。我想投票支持这一点(这是一个有助于增加知识库的答案),但我不能。
"the cat ran up the tree"
"the dog ran up the tree"
"the squirrel ran up the tree"
gsub("((\\w+\\s+\\w+\\s?)|(\\w+\\s+))\\1+", "\\1", vec)
#[1] "the cat ran up the tree"    
#[2]  "the dog ran up the tree"     
#[3] "the squirrel ran up the tree"