或者考虑将向量改为字符串,利用正则表达式进行操作,代码如下:
`
qq<-c("a","b","b","a","b","b","b","b",
"a","b","b","b","a")
qq_str<-paste0(qq,collapse = "")
library(stringr)
three_b<-str_locate_all(qq_str,pattern='b{3,}')
g<-function(x){return(seq(x[1],x[2],by=1))}
apply(three_b[[1]],1,g)
`
结果为
[[1]]
[1] 5 6 7 8
[[2]]
[1] 10 11 12