How to find the sum of consecutive values considering two values each time in R?

R ProgrammingServer Side ProgrammingProgramming

Finding the sum of consecutive value while considering the sum of two values each time means the sum of first two values, then the sum of second value and the third value, then the sum of third value and the fourth value, then the sum of fourth value and the fifth value, and so on. For this purpose, we can use rollapply function from zoo package.

Loading zoo package

ibrary(zoo)

Example

 Live Demo

x1<-1:3
x1

Output

[1] 1 2 3
rollapply(x1,2,sum)
[1] 3 5

Example

 Live Demo

x2<-1:10
x2

Output

[1] 1 2 3 4 5 6 7 8 9 10
rollapply(x2,2,sum)
[1] 3 5 7 9 11 13 15 17 19

Example

 Live Demo

x3<-sample(0:9,50,replace=TRUE)
x3

Output

[1] 4 5 0 0 5 4 9 2 1 9 7 2 0 5 3 7 6 6 1 6 3 7 0 4 0 5 4 4 7 3 8 0 3 7 2 2 1 7
[39] 2 2 1 7 2 5 0 9 1 4 3 6


rollapply(x3,2,sum)


[1] 9 5 0 5 9 13 11 3 10 16 9 2 5 8 10 13 12 7 7 9 10 7 4 4 5
[26] 9 8 11 10 11 8 3 10 9 4 3 8 9 4 3 8 9 7 5 9 10 5 7 9

Example

 Live Demo

x4<-rnorm(50,1,0.01)
x4

Output

[1] 0.9935449 1.0099022 0.9947295 1.0111667 0.9875526 1.0060585 1.0102984
[8] 1.0178206 0.9821054 1.0001210 0.9912770 0.9942123 1.0029275 0.9879245
[15] 0.9831371 0.9928732 0.9996728 1.0029515 0.9805963 0.9834781 1.0017181
[22] 0.9812307 1.0114344 1.0058302 1.0223386 0.9960463 1.0286982 1.0194191
[29] 0.9935153 1.0121636 0.9835419 1.0085877 0.9972304 0.9847358 1.0064163
[36] 0.9812214 1.0008247 1.0232812 1.0093367 0.9978258 1.0023659 0.9984375
[43] 1.0008212 0.9835083 0.9895553 0.9919702 0.9981046 0.9947893 1.0091464
[50] 1.0198059
rollapply(x4,2,sum)
[1] 1.994011 1.989994 1.983446 2.008251 1.995898 1.996479 2.005682 2.017292
[9] 2.042351 2.012531 1.995148 2.006504 2.005244 2.000492 2.006613 2.013892
[17] 2.003731 1.988736 1.989027 1.992109 2.009774 1.999826 1.999287 2.018762
[25] 2.001310 2.010054 2.021629 2.001857 1.978824 2.001365 2.017260 2.011697
[33] 1.992223 1.990860 2.020605 1.997287 1.985999 2.001785 1.992045 1.982278
[41] 1.979055 1.981131 1.998802 2.016486 2.017044 2.023902 2.020598 2.030335
[49] 2.038010

Example

 Live Demo

x5<-runif(50,2,3)
x5

Output

[1] 2.097345 2.471663 2.851550 2.743820 2.118416 2.769698 2.690867 2.246873
[9] 2.784047 2.258938 2.969015 2.477000 2.683269 2.697401 2.731411 2.191636
[17] 2.353095 2.993104 2.835925 2.211106 2.647715 2.436786 2.433908 2.627635
[25] 2.754094 2.043795 2.560132 2.459806 2.017324 2.516130 2.363325 2.793392
[33] 2.794425 2.545284 2.366587 2.881463 2.665224 2.587127 2.121832 2.433428
[41] 2.822085 2.206928 2.904388 2.987217 2.144524 2.711481 2.875176 2.066271
[49] 2.423833 2.333310
rollapply(x5,2,sum)
[1] 4.620479 4.987276 4.686020 4.620245 4.799499 4.855526 4.808018 4.911749
[9] 4.858226 4.303694 4.538936 4.570609 4.675954 4.981647 4.831803 4.970590
[17] 5.206684 5.111334 4.596743 4.732819 5.132689 4.747378 4.424908 4.653598
[25] 4.958530 4.760634 4.499325 4.858184 5.354862 5.466659 5.439993 5.067631
[33] 4.900857 5.060006 5.407719 5.684698 5.691366 5.232912 4.579391 5.058905
[41] 5.421667 4.535190 4.892816 5.030818 5.087761 5.523473 4.679963 4.910165
[49] 5.312161

Example

 Live Demo

x6<-round(runif(100,2,3),0)
x6

Output

[1] 2 3 2 2 2 3 3 3 3 2 2 2 3 2 2 3 3 2 3 3 2 2 2 3 3 2 2 2 2 3 3 2 3 3 3 2 3
[38] 2 2 3 2 3 2 2 2 3 2 3 3 3 2 3 3 3 2 3 3 3 2 3 2 2 3 3 3 3 2 3 3 3 2 3 2 2
[75] 2 3 2 2 2 3 3 3 3 3 2 2 3 2 3 2 2 3 2 3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11 10
[26] 10 15 16 11 9 12 14 10 10 12 10 7 7 7 7 7 7 6 9 9 6 7 10 8 6
[51] 7 8 12 13 18 15 8 8 11 14 7 6 12 9 6 7 6 6 7 13 16 8 9 15 14
[76] 12 9 12 16 15 11 11 13 12 14 17 17 15 11 9 11 10 10 11 7 9 11 9 8

Example

 Live Demo

x9<-sample(51:99,100,replace=TRUE)
x9

Output

[1] 81 80 78 97 58 73 74 76 67 72 87 70 94 61 63 93 96 63 63 90 76 56 98 84 95
[26] 90 95 58 87 56 95 65 87 99 56 86 72 91 89 79 98 59 66 81 88 59 52 81 54 69
[51] 86 63 64 67 53 71 69 61 67 85 99 81 88 87 80 54 80 93 95 74 87 95 54 53 62
[76] 73 56 75 83 57 72 93 75 61 60 66 76 96 66 65 77 71 73 68 92 91 63 76 70 64
rollapply(x9,2,sum)
[1] 115 116 156 190 169 164 165 159 160 177 186 180 183 175 156 126 140 140 109
[20] 135 168 147 148 141 134 175 149 124 165 157 136 151 125 124 171 153 129 125
[39] 127 168 154 123 126 143 164 182 176 174 184 179 172 148 143 138 126 130 142
[58] 147 157 169 137 138 148 117 117 142 136 133 131 157 198 150 127 155 167 179
[77] 171 173 171 141 160 167 166 149 140 184 162 128 115 128 160 171 155 151 145
[96] 156 151 108 143

Example

 Live Demo

x10<-sample(101:999,100,replace=TRUE)
x10

Output

[1] 583 340 524 234 269 932 926 919 195 968 192 736 964 529 185 286 127 530
[19] 170 452 941 676 393 352 257 845 958 436 198 581 509 724 857 311 252 530
[37] 384 501 880 494 553 979 195 747 877 839 233 162 877 510 161 784 810 904
[55] 967 507 837 832 505 896 371 385 563 579 112 545 800 967 999 124 505 710
[73] 357 927 614 684 272 726 484 276 693 378 252 329 171 532 519 187 177 943
[91] 821 186 871 881 386 644 113 406 551 619
rollapply(x10,2,sum)
[1] 656 1358 1359 724 1013 1727 1303 1342 1294 622 1142 1458 1045 823 475
[16] 902 979 629 911 817 1075 1120 688 507 384 827 970 1113 1240 758
[31] 1296 1384 624 741 956 743 942 1153 1436 1244 1297 1663 1579 1131 720
[46] 1345 1109 341 252 1028 1706 1470 805 461 1194 1169 1004 905 1018 1103
[61] 686 724 1201 1349 1150 958 969 841 892 1124 731 646 1275 1699 1462
[76] 1028 409 661 669 983 1089 361 404 624 1291 1358 1227 1428 699 229
[91] 893 1207 1270 1139 1028 1426 1470 1130 642
raja
Published on 08-Oct-2020 18:26:10
Advertisements