Skip to content
Prev 132657 / 398506 Next

Efficient way to find consecutive integers in vector?

In the R.utils package there is seqToIntervals(), e.g.

print(seqToIntervals(1:10))
##      from to
## [1,]    1 10
print(seqToIntervals(c(1:10, 15:18, 20)))
##      from to
## [1,]    1 10
## [2,]   15 18
## [3,]   20 20

There is also seqToIntervals(), which uses the above, e.g.

print(seqToHumanReadable(1:10))
## [1] "1-10"
print(seqToHumanReadable(c(1:10, 15:18, 20)))
## [1] "1-10, 15-18, 20"

/Henrik
On 21/12/2007, Tony Plate <tplate at acm.org> wrote: