Search code examples
regexrstrsplit

Split string by words in R


I would like to split a string by two words:

s <- "PCB153 treated HepG2 cells at T18"
strsplit(s, split = <treated><at>)

What should I write instead of <>?

I would get:

"PCB153" "HepG2 cells" "T18"

Solution

  • strsplit(s, split="treated|at")
    #[[1]]
    #[1] "PCB153 "       " HepG2 cells " " T18"