Run-length encoding captures the lengths of runs of consecutive elements in a vector. Consider an example vector:
dat <- c(1, 2, 2, 2, 3, 1, 4, 4, 1, 1)
The rle
function extracts each run and its length:
r <- rle(dat)
r
# Run Length Encoding
# lengths: int [1:6] 1 3 1 1 2 2
# values : num [1:6] 1 2 3 1 4 1
The values for each run are captured in r$values
:
r$values
# [1] 1 2 3 1 4 1
This captures that we first saw a run of 1's, then a run of 2's, then a run of 3's, then a run of 1's, and so on.
The lengths of each run are captured in r$lengths
:
r$lengths
# [1] 1 3 1 1 2 2
We see that the initial run of 1's was of length 1, the run of 2's that followed was of length 3, and so on.