The following vector x
contains the two sequences 1:4
and 6:7
, among other non-sequential digits.
x <- c(7, 1:4, 6:7, 9)
I'd like to split x
by its sequences, so that the result is a list like the following.
# [[1]]
# [1] 7
#
# [[2]]
# [1] 1 2 3 4
#
# [[3]]
# [1] 6 7
#
# [[4]]
# [1] 9
Is there a quick and simple way to do this?
I've tried
split(x, c(0, diff(x)))
which gets close, but I don't feel like appending 0
to the differenced vector is the right way to go. Using findInterval
didn't work either.