# How to find the number of runs in a sequence in R?

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

Sometimes data is recorded as a sequence of numerical values or strings and we might to find the frequency for each of the sequences. This helps us to check the variation in the runs but we must make sure the total frequency is equal to the total number values, otherwise our calculation of frequency would be incorrect. To find the number of runs, we can use rle function in R that stands for Run Length Encoding.

## Examples

x1<-rep(c(1:5),10)
x1
[1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3
[39] 4 5 1 2 3 4 5 1 2 3 4 5
rle(x1)
Run Length Encoding
lengths: int [1:50] 1 1 1 1 1 1 1 1 1 1 ...
values : int [1:50] 1 2 3 4 5 1 2 3 4 5 ...
x2<-rep(c(1:5),each=10)
x2
[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4
[39] 4 4 5 5 5 5 5 5 5 5 5 5
rle(x2)
Run Length Encoding
lengths: int [1:5] 10 10 10 10 10
values : int [1:5] 1 2 3 4 5
x3<-rep(c(2,3,4,5),times=c(20,14,18,21))
x3
[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4
[39] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
rle(x3)
Run Length Encoding
lengths: int [1:4] 20 14 18 21
values : num [1:4] 2 3 4 5
x4<-rep(c(0,1,1,0,1,0,1,0,1),times=c(4,8,6,4,9,16,7,8,12))
x4
[1] 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
[39] 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
rle(x4)
Run Length Encoding
lengths: int [1:8] 4 14 4 9 16 7 8 12
values : num [1:8] 0 1 0 1 0 1 0 1
x5<-rep(c(0,1,1,0,1,0,1,0,1),times=c(40,18,26,24,19,16,27,28,12))
x5
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[38] 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[75] 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
[112] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1
[149] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[186] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
rle(x5)
Run Length Encoding
lengths: int [1:8] 40 44 24 19 16 27 28 12
values : num [1:8] 0 1 0 1 0 1 0 1
x6<-rep(c(LETTERS[1:9]),times=c(40,18,26,24,19,16,27,28,12))
x6
[1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A"
[19] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A"
[37] "A" "A" "A" "A" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B"
[55] "B" "B" "B" "B" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C"
[73] "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "D" "D" "D" "D" "D" "D"
[91] "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D"
[109] "E" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E" "E"
[127] "E" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "G"
[145] "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G"
[163] "G" "G" "G" "G" "G" "G" "G" "G" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H"
[181] "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H"
[199] "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I" "I"
rle(x6)
Run Length Encoding
lengths: int [1:9] 40 18 26 24 19 16 27 28 12
values : chr [1:9] "A" "B" "C" "D" "E" "F" "G" "H" "I"
x7
[1] "India" "India" "India" "India"
[5] "India" "India" "India" "India"
[9] "India" "India" "India" "India"
[13] "India" "Russia" "Russia" "Russia"
[17] "Russia" "Russia" "Russia" "Russia"
[21] "Russia" "Russia" "Russia" "Russia"
[25] "China" "China" "China" "China"
[29] "China" "China" "China" "China"
[45] "Belarus" "Belarus" "Belarus" "Belarus"
[49] "Belarus" "Belarus" "Belarus" "Belarus"
[53] "Belarus" "Belarus" "Belarus" "Belarus"
[57] "Indonesia" "Indonesia" "Indonesia" "Indonesia"
[61] "Indonesia" "Indonesia" "Indonesia" "Indonesia"
[65] "Indonesia" "Indonesia" "Indonesia" "United Kingdom"
[69] "United Kingdom" "United Kingdom" "United Kingdom" "United Kingdom"
[73] "United Kingdom" "United Kingdom" "United Kingdom" "United Kingdom"
[77] "United Kingdom" "United Kingdom" "United Kingdom" "United Kingdom"
[81] "United Kingdom" "Sudan" "Sudan" "Sudan"
[85] "Sudan" "Sudan" "Sudan" "Sudan"
[89] "Sudan" "Sudan" "Sudan" "Sudan"
[93] "Sudan"
rle(x7)
Run Length Encoding
lengths: int [1:8] 13 11 10 10 12 11 14 12
values : chr [1:8] "India" "Russia" "China" "Canada" "Belarus" "Indonesia" ...
Updated on 24-Aug-2020 12:11:24