So, first take the diff
of a and do a run length sequence on it. Then, the starting points are the index before the 2s and the ending points are the negative differences of those... it's hard to explain, just step through the code and check it out. This does not find sequences of two... like (3,4) in (1, 3, 4, 7, 9). I had to include the remove
part for sequences that were off by two... (1, 3, 5, 7). Those weren't caught correctly. Any how, fun exercise. I hope somebody can do better. This is a bit of a mess...
data <- c(1,4,6,7,8,9,20,30,31,32,33,34,35,60)
a <- sequence(rle(diff(data))$lengths)
starts <- which(a==2) - 1
ends <- which(diff(a)<0) + 1
remove <- starts[starts %in% (ends-2)]
starts <- starts[!starts %in% remove]
ends <- ends[!ends %in% (remove+2)]
if(length(ends) < length(starts)) ends <- c(ends, length(data))
> starts
[1] 3 8
> ends
[1] 6 13
>