如何获得R中某些单词的前面部分?

时间:2017-03-21 15:15:07

标签: r

我试图在句子中注明某些单词的前面部分。 例如,句子是 -

x <- c("Ace Bayou Reannounces Recall of Bean Bag Chairs Due to Low Rate of Consumer Response; Two Child Deaths Previously Reported; Consumers Urged to Install Repair", "Panasonic Recalls Metal Cutter Saws Due to Laceration Hazard")

现在我想得到Recall或Recalls的前面部分。我已尝试过R中的各种函数,如grep,grepl,pmatch,str_split。但是,我无法得到我想要的东西。

我需要的只是

"Ace Bayou Reannounces"
"Panasonic"

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果你想使用正则表达式。

gsub(pattern = "(.*(?=Recall(s*)))(.*)", replacement = "\\1", x = x, perl = T)