我需要将一个以上有多个空格的字符串拆分为列表元素。假设我有以下字符串:
"My name is Bob and I like programming."
我尝试了以下代码:
library(stringr)
string <- "My name is Bob and I like programming."
strsplit(string,"\\s+")
哪个给出结果:
[[1]]
[1] "My" "name" "is"
[4] "Bob" "and" "I"
[7] "like" "programming."
但是,我想要这个结果:
[[1]]
[1] My name
[[2]]
[1] is Bob and
[[3]]
[1] I like programming.
感谢您的帮助!