【发布时间】:2018-02-26 21:40:15
【问题描述】:
我想提取某些关键字之前的字符串和关键字之后的第一个元素。给定以下字符串和关键字,
s <- c("E123Apple12", "EJ23ZGrape0Z", "J8BananaZ!")
keywords <- c("Apple", "Grape", "Banana")
我希望输出为:E123、EJ23Z 和 J8 用于关键字之前的字符串,1、0 和 Z 用于紧随其后出现的第一个元素关键字。
使用sub(keywords, "\\1", s) 会出现以下错误:
Warning message:
In sub(keywords, "\\", s) :
argument 'pattern' has length > 1 and only the first element will be used
【问题讨论】: