你可以使用
gsub(pattern, replacement, x)
其中 x 是您的字符变量,“pattern”是您要替换的单词,“replacement”是“”。但是,R 不知道代词是什么。所以你必须通过用你的字符串编写的语言编写所有可能的代词列表来告诉它。然后你必须重复删除所有代词(或任何类型的词),如下所示:
x <- "This is a character string in which I tell you how he deleted pronouns."
unwant <- c(
"I", "he", "she", "it",...)
unwanted <- c(paste(" ", unwanted, " ", sep = ""), paste(" ", unwanted, ".", sep = ""), paste(" ", unwanted, "!", sep = ""), paste(" ", unwanted, "?", sep = ""), paste(" ", unwanted, ",", sep = "")
)
result <- x
for(i in 1:NROW(unwanted)){
result <- gsub(unwanted[i], " ", result)
}
print(result)
显然,“...”意味着你必须插入所有你不想要的词,但我想互联网上的某个地方有所有代词的列表。
编辑:您必须在单词之前和之后插入空格,这样 R 就不会从它们出现的其他单词中删除这些字母。我通过paste 函数添加了这个,你的代词可以通过多种方式进行修改,例如以防它们出现在句末。