4

I have the following string and vector:

temp = "EarthMars Venus & Saturn PlanetsJupiter"

searchTerms = c("Earth", "Jupiter", "Mars", "Venus & Saturn Planets", "Neptune")

I want to split 'temp' based on the strings in 'searchTerms', so that I get the following:

result = c("Earth", "Mars", "Venus & Saturn Planets", "Jupiter") 

Thanks for the help!

3 Answers3

2

Using the stringr package, you could do:

library(stringr)
result = unlist(str_extract_all(temp,searchTerms))

[1] "Earth"          "Jupiter"         "Mars"           "Venus & Saturn Planets"
Lamia
  • 3,845
  • 1
  • 12
  • 19
1

One option similar to this post (R split on delimiter (split) keep the delimiter (split)) is:

searchStr <- paste0(searchTerms, collapse = "|")
unlist(strsplit(temp, paste0("(?<=",searchStr,")"), perl = T))

[1] "Earth" "Mars"  " Venus & Saturn Planets" "Jupiter"   
Mike H.
  • 13,960
  • 2
  • 29
  • 39
1

Another one-liner option with only base functions:

result <- unlist(lapply(searchTerms, function(x) regmatches(temp,regexpr(x,temp))))

Val
  • 6,585
  • 5
  • 22
  • 52