Pedro Fernández Pedro Fernández - 1 year ago 29
R Question

Counting chunk sizes in intermittent numerical sequences

My R problem is how to go from a numerical vector (or data frame) like:

a <- c(1,2,3,4,5,7,8,12,13,14,16,20)


Check for continuous sequences, retrieve them all, get their length, min and max and return something like this:

num count
1 1 5
2 5 5
3 7 2
4 8 2
5 12 3
6 14 3
7 16 1
8 20 1


Where
num
is the minimum and maximum of each segment, and
count
the length of that segment.

Grouping does not work on integers, so what can I do?

Answer Source

One way:

chunkID <- cumsum(c(TRUE, diff(a) > 1))
chunksize <- rle(chunkID)$lengths
chunkend <- cumsum(chunksize)
chunkstart <- chunkend - chunksize + 1

data.frame(num = c(rbind(a[chunkstart], a[chunkend])), count = rep(chunksize, each = 2))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download