【发布时间】:2020-02-17 05:15:10
【问题描述】:
我有一个包含一个或多个关键字的长字符串,在本例中为“Realm”。我一直在使用 gsub,但这只需要最后一个关键字之后的单词。
所以字符串看起来像:
...Attributes \r\n Realm - Afrotropical \r\n IUCN Ecosystem -- Terrestrial biome...
或
...Attributes \r\n Realm - Afrotropical \r\n Realm - Neotropical \r\n . IUCN Ecosystem -- Terrestrial biome...
我一直在使用该功能:
Realm_fun<-function(x){gsub('^.*Realm -\\s*|\\s*IUCN Ecosystem.*$', '', x)}
然后使用lapply 在所有字符串上进行有趣的操作。
我该怎么做才能获得Afrotropical 的第一个字符串和Afrotropical , Neotropical 的第二个?
【问题讨论】: