Create grouping variable for consecutive sequences and split vector

Making heavy use of some R idioms:

> split(v, cumsum(c(1, diff(v) != 1)))
$`1`
[1] 1

$`2`
[1] 3 4 5

$`3`
[1]  9 10

$`4`
[1] 17

$`5`
[1] 29 30

Leave a Comment