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 Answers 3

2

Using the stringr package, you could do:

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

[1] "Earth"          "Jupiter"         "Mars"           "Venus & Saturn Planets"
Sign up to request clarification or add additional context in comments.

Comments

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"   

Comments

1

Another one-liner option with only base functions:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.