**Q**: Given a sequence of random numbers, find the start and end positions of runs of five or more consecutive numbers that are greater than zero.

**A**: Use the `rle()`

function.

For example, let’s apply `rle()`

to the following sequence of numbers.

We see that `rle()`

returns a list of two elements: lengths and values, where the latter gives the unique number of each run, and the former gives the run length, i.e. the number of consecutive repeats within each run. For example, the first run is the number 2 repeated 5 times, and the second run is the number 5 repeated once.

Let’s solve the original question. First, we set the seed, generate a sequence of normal random numbers greater than zero and apply `rle()`

to it.

Next, we find indices of the runs with length of at least 5.

Next, we can do a cumulative sum of the run lengths and extract the end positions of the runs with length of at least 5 using the above found indices.

Next, we find the start positions of these runs.

Lastly, we print out the start and end positions of these runs and use them to extract the runs themselves.

To see more examples of how to use `rle()`

, read Winston Chang’s post.