用R中的单词拆分字符串

时间:2021-01-20 21:35:59

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"

2 个解决方案

#1


9  

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

#2


1  

You have to enter it as a string. To split on treated:

您必须将其作为字符串输入。拆分处理:

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

To split on treated and at:

分开处理和在:

unlist(strsplit(unlist(s2),split="at"))

#1


9  

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

#2


1  

You have to enter it as a string. To split on treated:

您必须将其作为字符串输入。拆分处理:

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

To split on treated and at:

分开处理和在:

unlist(strsplit(unlist(s2),split="at"))