Analyse vector sequences

rle
Author
Affiliations

Layal Christine Lettry

cynkra GmbH

University of Fribourg, Dept. of Informatics, ASAM Group

Published

March 28, 2024

How can you easily analyse vector sequences in base R?

Run-length encoding

Flying over the GitHub repository about useful and powerful shortcuts in base R, I stumbled upon an easy way to split a vector into sequences of equal values.

The function rle() allows you to deconstruct a vector. This function shows the frequency of an instance in a certain sequence. If a value is followed by the same one, the rle() function will count the number of these successive instances.

The rle() function returns a list of the values and their respective lengths in a vector sequence. The class of this list is "rle".

x <- c(1L, 5L, 7L, 2L, 2L, 7L, 7L, 7L, 8L, 1L)
(y <- rle(x))
Run Length Encoding
  lengths: int [1:7] 1 1 1 2 3 1 1
  values : int [1:7] 1 5 7 2 7 8 1
tidyr::tibble(
  length_x = y[["lengths"]], values_x = y[["values"]]
)
# A tibble: 7 × 2
  length_x values_x
     <int>    <int>
1        1        1
2        1        5
3        1        7
4        2        2
5        3        7
6        1        8
7        1        1
typeof(y)
[1] "list"
class(y)
[1] "rle"
w <- c(2.0, 3.0, 8.0, 8.0, 5.0, 5.0, 5.0, 7.0, 9.0, 9.0)
(z <- rle(w))
Run Length Encoding
  lengths: int [1:6] 1 1 2 3 1 2
  values : num [1:6] 2 3 8 5 7 9
tidyr::tibble(
  length_w = z[["lengths"]], values_w = z[["values"]]
)
# A tibble: 6 × 2
  length_w values_w
     <int>    <dbl>
1        1        2
2        1        3
3        2        8
4        3        5
5        1        7
6        2        9
typeof(z)
[1] "list"
class(z)
[1] "rle"
m <- c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE)
(n <- rle(m))
Run Length Encoding
  lengths: int [1:4] 3 2 2 1
  values : logi [1:4] FALSE TRUE FALSE TRUE
tidyr::tibble(
  length_m = n[["lengths"]], values_m = n[["values"]]
)
# A tibble: 4 × 2
  length_m values_m
     <int> <lgl>   
1        3 FALSE   
2        2 TRUE    
3        2 FALSE   
4        1 TRUE    
typeof(n)
[1] "list"
class(n)
[1] "rle"

Reverse operation

You can get the vector back with inverse.rle().

(inv_y <- inverse.rle(y))
 [1] 1 5 7 2 2 7 7 7 8 1
typeof(inv_y)
[1] "integer"
class(inv_y)
[1] "integer"
(inv_z <- inverse.rle(z))
 [1] 2 3 8 8 5 5 5 7 9 9
typeof(inv_z)
[1] "double"
class(inv_z)
[1] "numeric"
(inv_n <- inverse.rle(n))
[1] FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE
typeof(inv_n)
[1] "logical"
class(inv_n)
[1] "logical"

Citation

BibTeX citation:
@online{lettry2024,
  author = {Lettry, Layal Christine},
  title = {Analyse Vector Sequences},
  date = {2024-03-28},
  url = {https://rdiscovery.netlify.app/posts/2024-03-28_rle/},
  langid = {en}
}
For attribution, please cite this work as:
Lettry, Layal Christine. 2024. “Analyse Vector Sequences.” March 28, 2024. https://rdiscovery.netlify.app/posts/2024-03-28_rle/.