Similar to Finding groups of contiguous numbers in a list and Find consecutive values in vector in R, the difference being I have a sequence of numbers that may be up to 4, ie
y=c(4*1:4,24,31,33,39,4*16:20)
> y
[1] 4 8 12 16 24 31 33 39 64 68 72 76 80
And I would like a function to return every sequence of values which are separated by a maximum of 4, so I get:
4 8 12 16 # all sep by at most 4
31 33 # all sep by at most 4
64 68 72 76 80 # all sep by at most 4
I tried:
st=c(1,which(diff(y)<5)+1)
en=c(st-1,length(y))
y[st]
[1] 4 8 12 16 33 68 72 76 80
y[en]
[1] 4 8 12 31 64 68 72 76 80
to no avail.
I am sure I am missing something obvious, and would appreciate any hints.