How to return the same index for consecutively duplicated values in an R vector?

R ProgrammingServer Side ProgrammingProgramming

It is obvious that duplicate values in an R vector do not have same indexes but we might want to create the same index for consecutively duplicated values, this will help to recognize the groups of duplicated values. For this purpose, we can use cumsum function along with diff function as shown in the below examples.

Example1

 Live Demo

x1<−sample(0:2,20,replace=TRUE)
x1

Output

[1] 1 1 0 2 0 0 2 2 1 1 0 1 2 1 0 2 1 2 1 1

Example

cumsum(c(1,diff(x1)!=0))
[1] 1 1 2 3 4 4 5 5 6 6 7 8 9 10 11 12 13 14 15 15

Example2

 Live Demo

x2<−rpois(200,2)
x2

Output

[1] 1 2 2 1 2 0 1 2 1 1 2 2 2 2 1 1 1 3 2 0 0 0 1 2 2 0 2 0 2 1 1 5 2 1 1 2 2
[38] 1 3 1 1 0 3 2 1 2 4 2 1 0 3 1 1 2 1 1 2 3 2 5 6 2 6 1 2 2 3 2 0 2 0 1 0 1
[75] 1 5 4 1 1 2 2 0 0 2 3 2 2 3 2 1 2 1 1 1 2 1 0 4 4 3 1 1 3 1 0 2 2 2 1 0 0
[112] 3 4 0 2 2 3 2 3 1 6 4 1 5 7 3 3 0 5 2 0 2 3 2 1 3 2 2 3 0 1 2 3 2 1 1 1 1
[149] 1 2 2 3 3 3 2 1 4 2 1 4 2 3 1 1 2 4 1 0 3 3 2 1 1 3 3 1 1 1 1 4 4 3 1 2 1
[186] 4 0 2 1 1 2 2 0 0 1 1 5 4 3 2

Example

cumsum(c(1,diff(x2)!=0))

Output

[1] 1 2 2 3 4 5 6 7 8 8 9 9 9 9 10 10 10 11
[19] 12 13 13 13 14 15 15 16 17 18 19 20 20 21 22 23 23 24
[37] 24 25 26 27 27 28 29 30 31 32 33 34 35 36 37 38 38 39
[55] 40 40 41 42 43 44 45 46 47 48 49 49 50 51 52 53 54 55
[73] 56 57 57 58 59 60 60 61 61 62 62 63 64 65 65 66 67 68
[91] 69 70 70 70 71 72 73 74 74 75 76 76 77 78 79 80 80 80
[109] 81 82 82 83 84 85 86 86 87 88 89 90 91 92 93 94 95 96
[127] 96 97 98 99 100 101 102 103 104 105 106 106 107 108 109 110 111 112
[145] 113 113 113 113 113 114 114 115 115 115 116 117 118 119 120 121 122 123
[163] 124 124 125 126 127 128 129 129 130 131 131 132 132 133 133 133 133 134
[181] 134 135 136 137 138 139 140 141 142 142 143 143 144 144 145 145 146 147
[199] 148 149

Example3

cumsum(c(1,diff(x3)!=0))

Output

[1] 1 1 2 2 3 4 5 6 7 7 8 9 10 11 12 13 13 13
[19] 13 14 15 16 17 18 19 20 21 21 22 23 23 24 25 26 27 28
[37] 28 29 30 31 31 32 33 34 35 36 36 37 38 39 39 39 39 40
[55] 41 41 42 42 43 44 45 46 46 47 48 49 50 51 51 52 53 54
[73] 55 56 57 58 59 59 59 60 61 61 61 61 61 61 62 62 62 63
[91] 64 65 66 67 68 68 69 70 71 72 72 73 74 75 76 77 78 79
[109] 80 81 82 82 82 83 84 84 85 86 87 88 89 89 90 90 90 91
[127] 91 92 93 93 94 95 96 96 97 98 98 98 98 99 100 101 101 102
[145] 102 103 104 105 106 107 108 109 109 110 111 111 111 112 113 114 114 114
[163] 115 116 117 118 118 119 120 120 120 120 121 122 123 124 125 126 127 128
[181] 129 130 130 130 131 132 133 134 135 135 136 137 138 139 140 141 142 143
[199] 143 144

Example4

 Live Demo

x4<−round(rnorm(200,5,1.2),0)
x4

Output

[1] 5 6 5 3 6 6 6 3 6 5 7 4 4 5 4 4 6 3 3 4 5 6 6 5 6 6 6 6 6 7 2 4 7 6 7 7 6
[38] 5 6 4 3 6 6 4 5 6 5 6 4 4 5 3 6 4 5 8 5 8 7 6 3 3 6 3 4 4 5 5 5 6 6 5 4 4
[75] 5 6 7 5 4 3 7 5 4 6 7 4 4 3 3 7 4 6 5 5 5 6 4 5 4 3 6 5 4 5 6 4 4 5 4 4 4
[112] 5 7 5 6 5 4 6 4 5 6 5 7 5 3 3 5 5 5 3 7 6 4 3 5 5 6 5 5 6 5 4 6 5 5 5 5 4
[149] 4 4 5 7 5 4 5 6 4 5 4 6 5 3 5 5 5 6 3 5 7 7 4 5 5 5 4 5 6 5 3 5 4 4 6 5 3
[186] 4 6 6 5 5 5 5 7 4 5 4 5 5 4 6

Example

cumsum(c(1,diff(x4)!=0))

Output

[1] 1 2 3 4 5 5 5 6 7 8 9 10 10 11 12 12 13 14
[19] 14 15 16 17 17 18 19 19 19 19 19 20 21 22 23 24 25 25
[37] 26 27 28 29 30 31 31 32 33 34 35 36 37 37 38 39 40 41
[55] 42 43 44 45 46 47 48 48 49 50 51 51 52 52 52 53 53 54
[73] 55 55 56 57 58 59 60 61 62 63 64 65 66 67 67 68 68 69
[91] 70 71 72 72 72 73 74 75 76 77 78 79 80 81 82 83 83 84
[109] 85 85 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 99
[127] 100 100 100 101 102 103 104 105 106 106 107 108 108 109 110 111 112 113
[145] 113 113 113 114 114 114 115 116 117 118 119 120 121 122 123 124 125 126
[163] 127 127 127 128 129 130 131 131 132 133 133 133 134 135 136 137 138 139
[181] 140 140 141 142 143 144 145 145 146 146 146 146 147 148 149 150 151 151
[199] 152 153

Example5

 Live Demo

x5<−sample(0:5,200,replace=TRUE)
x5

Output

[1] 0 3 0 5 0 4 2 0 5 4 2 4 2 3 0 1 1 0 5 4 0 0 0 5 5 0 5 3 2 4 0 5 0 2 3 4 5
[38] 5 2 2 0 5 1 3 0 1 2 3 0 0 4 5 3 5 3 4 0 3 5 5 5 5 3 4 1 5 3 4 3 1 5 2 3 1
[75] 1 5 1 3 3 2 4 2 2 4 3 5 0 4 3 4 1 0 4 1 5 2 2 5 3 5 1 0 2 3 5 2 2 3 5 3 2
[112] 4 3 4 4 5 3 4 1 0 2 5 5 5 1 0 3 2 0 0 3 4 5 0 3 0 4 2 3 5 3 1 0 5 0 0 3 4
[149] 3 0 5 2 1 1 2 4 0 0 3 3 2 5 0 5 2 1 5 4 1 1 5 5 5 0 3 5 2 2 5 0 2 1 5 2 5
[186] 3 1 0 1 1 4 2 3 4 0 1 0 0 4 0

Example

cumsum(c(1,diff(x5)!=0))

Output

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 16 17
[19] 18 19 20 20 20 21 21 22 23 24 25 26 27 28 29 30 31 32
[37] 33 33 34 34 35 36 37 38 39 40 41 42 43 43 44 45 46 47
[55] 48 49 50 51 52 52 52 52 53 54 55 56 57 58 59 60 61 62
[73] 63 64 64 65 66 67 67 68 69 70 70 71 72 73 74 75 76 77
[91] 78 79 80 81 82 83 83 84 85 86 87 88 89 90 91 92 92 93
[109] 94 95 96 97 98 99 99 100 101 102 103 104 105 106 106 106 107 108
[127] 109 110 111 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
[145] 126 126 127 128 129 130 131 132 133 133 134 135 136 136 137 137 138 139
[163] 140 141 142 143 144 145 146 146 147 147 147 148 149 150 151 151 152 153
[181] 154 155 156 157 158 159 160 161 162 162 163 164 165 166 167 168 169 169
[199] 170 171
raja
Updated on 09-Feb-2021 12:33:07

Advertisements