Sum of intervals lengths from an integer vector

Viewed 837

Let's say I have this integer vector:

> int.vec
 [1]  1  2  3  5  6  7 10 11 12 13

(created from int.vec <- c(1:3,5:7,10:13))

I'm looking for a function that will return the sum of the lengths of all intervals in this vector.

So basically for int.vec this function will return:

3+3+4 = 10
3 Answers
Related