R - run length encoding with rle()

10 Feb 2021

rle() from base

Run Length Encoding:

Compute the lengths and values of runs of equal values in a vector – or the reverse operation.


x <- c(1,1,3,4,4,4,5,5,6)

rle(x)
# Run Length Encoding
#   lengths: int [1:5] 2 1 3 2 1
#   values : num [1:5] 1 3 4 5 6

rle(x)$lengths
# [1] 2 1 3 2 1
rle(x)$values
# [1] 1 3 4 5 6

[ R  function  base  ]