Problem: Given an atomic vector, find the start and end indices of runs in the vector.
Example vector with runs:
x = rev(rep(6:10, 1:5))
# [1] 10 10 10 10 10 9 9 9 9 8 8 8 7 7 6
Output from rle()
:
rle(x)
# Run Length Encoding
# lengths: int [1:5] 5 4 3 2 1
# values : int [1:5] 10 9 8 7 6
Desired output:
# start end
# 1 1 5
# 2 6 9
# 3 10 12
# 4 13 14
# 5 15 15
The base rle
class doesn't appear to provide this functionality, but the class Rle
and function rle2
do. However, given how minor the functionality is, sticking to base R seems more sensible than installing and loading additional packages.
There are examples of code snippets (here, here and on SO) which solve the slightly different problem of finding start and end indices for runs which satisfy some condition. I wanted something that would be more general, could be performed in one line, and didn't involve the assignment of temporary variables or values.
Answering my own question because I was frustrated by the lack of search results. I hope this helps somebody!
dplyr::lag
? In your second solution, do meanselect
instead ofextract
(fromtidyr
?) ? Cheers – Leadin