Add values in sequence to the previous value with a constant.


To add values in sequence to the previous value with a constant, we can find the cumulative sum of values and add the constant to the Output.

For Example, if we have a vector called X and we want to add values in X in sequence to the previous value with a constant say 10 then we can use the command given below −

10+cumsum(X)

Example 1

Following snippet creates a sample data frame −

x1<-1:100
cumsum(x1)

The following dataframe is created

[1] 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120
[16] 136 153 171 190 210 231 253 276 300 325 351 378 406 435
252
[31] 496 528 561 595 630 666 703 741 780 820 861 903 946 990
1035
[46] 1081 1128 1176 1225 1275 1326 1378 1431 1485 1540 1596 1653 1711 1770
1830
[61] 1891 1953 2016 2080 2145 2211 2278 2346 2415 2485 2556 2628 2701 2775
2850
[76] 2926 3003 3081 3160 3240 3321 3403 3486 3570 3655 3741 3828 3916 4005
4095
[91] 4186 4278 4371 4465 4560 4656 4753 4851 4950 5050

Add the following code to the above snippet −

x1<-1:100
x1_new<-1+cumsum(x1)
x1_new

Output

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

[1] 2 4 7 11 16 22 29 37 46 56 67 79 92 106 121
[16] 137 154 172 191 211 232 254 277 301 326 352 379 407 436
466
[31] 497 529 562 596 631 667 704 742 781 821 862 904 947 991
1036
[46] 1082 1129 1177 1226 1276 1327 1379 1432 1486 1541 1597 1654 1712 1771
1831
[61] 1892 1954 2017 2081 2146 2212 2279 2347 2416 2486 2557 2629 2702 2776
2851
[76] 2927 3004 3082 3161 3241 3322 3404 3487 3571 3656 3742 3829 3917 4006
4096
[91] 4187 4279 4372 4466 4561 4657 4754 4852 4951 5051

Example 2

Following snippet creates a sample data frame −

x2<-rpois(100,10)
x2

The following dataframe is created

[1] 8 15 5 14 4 11 19 9 7 9 6 11 8 8 8 5 6 6 12 11 6 12 8 12 8
[26] 7 11 5 6 14 9 12 12 12 5 13 10 15 13 10 12 15 7 13 6 10 7 9 13
14
[51] 15 7 8 11 11 10 10 5 13 13 7 13 9 11 7 9 7 10 5 9 11 13 15 17
8
[76] 15 13 12 10 11 3 10 11 10 12 8 18 10 11 13 6 7 8 11 14 9 11 10 9
9

Add the following code to the above snippet −

x2<-rpois(100,10)
cumsum(x2)

Output

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

[1] 8 23 28 42 46 57 76 85 92 101 107 118 126 134 142
[16] 147 153 159 171 182 188 200 208 220 228 235 246 251 257
271
[31] 280 292 304 316 321 334 344 359 372 382 394 409 416 429
435
[46] 445 452 461 474 488 503 510 518 529 540 550 560 565 578
591
[61] 598 611 620 631 638 647 654 664 669 678 689 702 717 734
742
[76] 757 770 782 792 803 806 816 827 837 849 857 875 885 896
909
[91] 915 922 930 941 955 964 975 985 994 1003

Add the following code to the above snippet −

x2<-rpois(100,10)
x2_new<-1+cumsum(x2)
x2_new

Output

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

[1] 9 24 29 43 47 58 77 86 93 102 108 119 127 135 143
[16] 148 154 160 172 183 189 201 209 221 229 236 247 252 258
272
[31] 281 293 305 317 322 335 345 360 373 383 395 410 417 430
436
[46] 446 453 462 475 489 504 511 519 530 541 551 561 566 579
592
[61] 599 612 621 632 639 648 655 665 670 679 690 703 718 735
743
[76] 758 771 783 793 804 807 817 828 838 850 858 876 886 897
910
[91] 916 923 931 942 956 965 976 986 995 1004

Example 3

Following snippet creates a sample data frame −

x3<-rpois(100,1)
x3

The following dataframe is created

[1] 1 2 4 0 1 0 2 2 0 0 0 0 0 1 0 1 1 0 1 3 1 2 1 3 0 0 0 0 0 0 1 0 1 1 1 0 0
[38] 4 1 1 2 0 0 0 5 1 2 1 2 0 0 3 0 1 0 0 0 0 1 0 1 1 1 0 2 2 0 1 1 1 0 0 2 0
[75] 1 3 0 0 1 0 0 1 0 0 1 0 1 1 0 3 2 0 2 1 1 1 3 0 1 0

Add the following code to the above snippet −

x3<-rpois(100,1)
x3
cumsum(x3)

Output

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

[1] 1 3 7 7 8 8 10 12 12 12 12 12 12 13 13 14 15 15 16 19 20 22 23 26 26
[26] 26 26 26 26 26 27 27 28 29 30 30 30 34 35 36 38 38 38 38 43 44 46 47 49
49
[51] 49 52 52 53 53 53 53 53 54 54 55 56 57 57 59 61 61 62 63 64 64 64 66 66
67
[76] 70 70 70 71 71 71 72 72 72 73 73 74 75 75 78 80 80 82 83 84 85 88 88 89
89

Add the following code to the above snippet −

x3<-rpois(100,1)
x3_new<-100+cumsum(x3)
x3_new

Output

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

[1] 101 103 107 107 108 108 110 112 112 112 112 112 112 113 113 114 115 115
[19] 116 119 120 122 123 126 126 126 126 126 126 126 127 127 128 129 130 130
[37] 130 134 135 136 138 138 138 138 143 144 146 147 149 149 149 152 152 153
[55] 153 153 153 153 154 154 155 156 157 157 159 161 161 162 163 164 164 164
[73] 166 166 167 170 170 170 171 171 171 172 172 172 173 173 174 175 175 178
[91] 180 180 182 183 184 185 188 188 189 189

Example 4

Following snippet creates a sample data frame −


x4<-sample(1:100,100)
x4

The following dataframe is created

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

Add the following code to the above snippet −

x4<-sample(1:100,100)
cumsum(x4)

Output

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

[1] 74 98 141 181 215 312 342 388 413 479 567 612 697 760 809
[16] 861 928 997 1033 1104 1143 1229 1283 1353 1364 1424 1492 1583 1620
1623
[31] 1681 1746 1753 1814 1835 1861 1934 2016 2045 2109 2119 2141 2240 2253
2329
[46] 2413 2432 2433 2505 2583 2589 2607 2612 2654 2656 2713 2802 2900 2904
2912
[61] 2945 2957 2971 3065 3148 3248 3298 3342 3370 3449 3502 3533 3608 3685
3772
[76] 3828 3890 3910 3961 4051 4131 4172 4207 4255 4272 4353 4369 4401 4493
4548
[91] 4557 4604 4627 4642 4669 4762 4800 4896 4955 5050

Add the following code to the above snippet −

x4<-sample(1:100,100)
x4_new<-1+cumsum(x4)
x4_new

Output

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

[1] 75 99 142 182 216 313 343 389 414 480 568 613 698 761 810
[16] 862 929 998 1034 1105 1144 1230 1284 1354 1365 1425 1493 1584 1621
1624
[31] 1682 1747 1754 1815 1836 1862 1935 2017 2046 2110 2120 2142 2241 2254
2330
[46] 2414 2433 2434 2506 2584 2590 2608 2613 2655 2657 2714 2803 2901 2905
2913
[61] 2946 2958 2972 3066 3149 3249 3299 3343 3371 3450 3503 3534 3609 3686
3773
[76] 3829 3891 3911 3962 4052 4132 4173 4208 4256 4273 4354 4370 4402 4494
4549
[91] 4558 4605 4628 4643 4670 4763 4801 4897 4956 5051

Example 5

Following snippet creates a sample data frame −

x5<-round(rnorm(80),2)
x5

The following dataframe is created

[1] 1.61 0.31 1.60 0.87 0.60 -0.41 -2.44 1.51 0.24 -0.15 -1.12 0.36
[13] 0.18 -1.09 0.97 -0.15 -0.15 -0.64 1.28 1.61 -0.04 0.62 0.38 1.50
[25] 0.15 2.10 0.32 0.34 0.79 1.11 0.47 1.26 -1.49 -0.13 0.58 0.73
[37] 0.71 -0.16 0.29 -0.63 -0.96 -1.80 0.43 0.21 -0.33 -0.35 -1.64 -0.59
[49] 0.10 -0.12 -0.30 -0.70 0.92 -0.80 -1.71 1.99 -0.79 0.80 -0.13 0.39
[61] 0.19 0.16 0.71 -0.28 -0.93 0.38 2.65 0.43 -1.46 2.49 0.73 -1.38
[73] -0.43 0.40 -0.74 -1.29 -0.35 0.55 -0.33 1.63

Add the following code to the above snippet −

x5<-round(rnorm(80),2)
cumsum(x5)

Output

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

[1] 1.61 1.92 3.52 4.39 4.99 4.58 2.14 3.65 3.89 3.74 2.62 2.98
[13] 3.16 2.07 3.04 2.89 2.74 2.10 3.38 4.99 4.95 5.57 5.95 7.45
[25] 7.60 9.70 10.02 10.36 11.15 12.26 12.73 13.99 12.50 12.37 12.95 13.68
[37] 14.39 14.23 14.52 13.89 12.93 11.13 11.56 11.77 11.44 11.09 9.45 8.86
[49] 8.96 8.84 8.54 7.84 8.76 7.96 6.25 8.24 7.45 8.25 8.12 8.51
[61] 8.70 8.86 9.57 9.29 8.36 8.74 11.39 11.82 10.36 12.85 13.58 12.20
[73] 11.77 12.17 11.43 10.14 9.79 10.34 10.01 11.64

Add the following code to the above snippet −

x5<-round(rnorm(80),2)
x5_new<-1+cumsum(x5)
x5_new

Output

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

[1] 2.61 2.92 4.52 5.39 5.99 5.58 3.14 4.65 4.89 4.74 3.62 3.98
[13] 4.16 3.07 4.04 3.89 3.74 3.10 4.38 5.99 5.95 6.57 6.95 8.45
[25] 8.60 10.70 11.02 11.36 12.15 13.26 13.73 14.99 13.50 13.37 13.95 14.68
[37] 15.39 15.23 15.52 14.89 13.93 12.13 12.56 12.77 12.44 12.09 10.45 9.86
[49] 9.96 9.84 9.54 8.84 9.76 8.96 7.25 9.24 8.45 9.25 9.12 9.51
[61] 9.70 9.86 10.57 10.29 9.36 9.74 12.39 12.82 11.36 13.85 14.58 13.20
[73] 12.77 13.17 12.43 11.14 10.79 11.34 11.01 12.64

Updated on: 02-Nov-2021

398 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements