How to split a vector into smaller vectors of consecutive values in R?


To split a vector into smaller vectors of consecutive values, we can use split function with cumsum function.

For example, if we have a vector called X that contains some consecutive values then we split X into smaller vectors of consecutive values by using the command given below −

split(x,cumsum(c(1,diff(x)!=1)))

Example 1

Following snippet creates a vector −

x1<-c(1:10,11:18,27:40,51:60,71:99,101:111,126:136,158:173,211:222,289:305,321:334)
x1

The following vector is created −

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 27 28 29 30 31 32 33 34 35 36 37 38 39 40 51 52 53 54
[37] 55 56 57 58 59 60 71 72 73 74 75 76 77 78 79 80 81 82
[55] 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 101
[73] 102 103 104 105 106 107 108 109 110 111 126 127 128 129 130 131 132 133
[91] 134 135 136 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
[109] 173 211 212 213 214 215 216 217 218 219 220 221 222 289 290 291 292 293
[127] 294 295 296 297 298 299 300 301 302 303 304 305 321 322 323 324 325 326
[145] 327 328 329 330 331 332 333 334

In order to split x1 into smaller vectors of consecutive values, add the following code to the above snippet −

x1<-c(1:10,11:18,27:40,51:60,71:99,101:111,126:136,158:173,211:222,289:305,321:334)
split(x1,cumsum(c(1,diff(x1)!=1)))

Output

If you execute all the above given snippets as a single program, it generates the following output: −

$`1`
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

$`2`
[1] 27 28 29 30 31 32 33 34 35 36 37 38 39 40

$`3`
[1] 51 52 53 54 55 56 57 58 59 60

$`4`
[1] 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
[26] 96 97 98 99

$`5`
[1] 101 102 103 104 105 106 107 108 109 110 111

$`6`
[1] 126 127 128 129 130 131 132 133 134 135 136

$`7`
[1] 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

$`8`
[1] 211 212 213 214 215 216 217 218 219 220 221 222

$`9`
[1] 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

$`10`
[1] 321 322 323 324 325 326 327 328 329 330 331 332 333 334

Example 2

Following snippet creates a vector −

x2<-c(-100:-89,-79:-65,-50:-41,-31:-21,-15:-1,0:10,15:18,22:40,55:67,75:89,96:105)
x2

The following vector is created −

[1] -100 -99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 -79 -78 -77
[16] -76 -75 -74 -73 -72 -71 -70 -69 -68 -67 -66 -65 -50 -49 -48
[31] -47 -46 -45 -44 -43 -42 -41 -31 -30 -29 -28 -27 -26 -25 -24
[46] -23 -22 -21 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4
[61] -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 15
[76] 16 17 18 22 23 24 25 26 27 28 29 30 31 32 33
[91] 34 35 36 37 38 39 40 55 56 57 58 59 60 61 62
[106] 63 64 65 66 67 75 76 77 78 79 80 81 82 83 84
[121] 85 86 87 88 89 96 97 98 99 100 101 102 103 104 105

In order to split x2 into smaller vectors of consecutive values, add the following code to the above snippet −

x2<-c(-100:-89,-79:-65,-50:-41,-31:-21,-15:-1,0:10,15:18,22:40,55:67,75:89,96:105)
split(x2,cumsum(c(1,diff(x2)!=1)))

Output

If you execute all the above given snippets as a single program, it generates the following output−

$`1`
[1] -100 -99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89

$`2`
[1] -79 -78 -77 -76 -75 -74 -73 -72 -71 -70 -69 -68 -67 -66 -65

$`3`
[1] -50 -49 -48 -47 -46 -45 -44 -43 -42 -41

$`4`
[1] -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21

$`5`
[1] -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3
[20] 4 5 6 7 8 9 10

$`6`
[1] 15 16 17 18

$`7`
[1] 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

$`8`
[1] 55 56 57 58 59 60 61 62 63 64 65 66 67

$`9`
[1] 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

$`10`
[1] 96 97 98 99 100 101 102 103 104 105

Updated on: 10-Nov-2021

575 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements