如何找到R中序列的运行次数?
有时,数据被记录为一个数字值或字符串序列,我们可能会找到每个序列的频率。这有助于我们检查运行中的变化,但必须确保总频率等于总数值,否则我们的频率计算将不正确。要找到运行次数,我们可以在R中使用rle函数代表运行长度编码。
例子
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<-rep(c("India","Russia","China","Canada","Belarus","Indonesia","United Kingdom","Sudan"),times=c(13,11,10,10,12,11,14,12)) 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" [33] "China" "China" "Canada" "Canada" [37] "Canada" "Canada" "Canada" "Canada" [41] "Canada" "Canada" "Canada" "Canada" [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" ...