rle(x)
x
lengths
values
x <- rev(rep(6:10, 1:5)) rle(x) ## $lengths ## [1] 5 4 3 2 1 ## $values ## [1] 10 9 8 7 6