我有一个如下所示的字符串。
a <- "T,2016,07,T,2016,07,22,T,2016,07"
我想把它分开来得到这个,
b <- c("T,2016,07", "T,2016,07", "T,2016,07")
你可以告诉我这个方法吗?非常感谢。
答案 0 :(得分:7)
或使用正则表达式进行拆分:
strsplit(a, ",(?=T)", perl = T)
# [[1]]
# [1] "T,2016,07" "T,2016,07,22" "T,2016,07"
答案 1 :(得分:3)
你可以做到
x <- gsub("T", "%T", a)
y <- unlist(strsplit(x, "%"))[-1]
答案 2 :(得分:2)
a <- "T,2016,07,T,2016,07,22,T,2016,07"
paste0("T", Filter(nzchar, strsplit(a, ",?T")[[1]]))
# [1] "T,2016,07" "T,2016,07,22" "T,2016,07"