# How to find the cumulative sums if a vector contains NA values in R?

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

The cumulative sums are the sum of consecutive values and we can take this sum for any numerical vector or a column of an R data frame. But if there exits an NA, then we need to skip it and therefore the size of the cumulative sums will be reduced by the number of NA values. If we have NA values in a vector then we can ignore them while calculating the cumulative sums with cumsum function by using !is.na.

## Examples

Live Demo

> x1<-c(1:10,NA)
> x1

## Output

[1] 1 2 3 4 5 6 7 8 9 10 NA

> cumsum(x1[!is.na(x1)])
[1] 1 3 6 10 15 21 28 36 45 55

## Example

Live Demo

> x2<-sample(1:10,50,replace=TRUE)
> x2

## Output

[1] 10 5 4 1 2 3 9 5 8 10 10 3 8 4 8 7 6 8 2 4 4 9 7 7 9
[26] 9 3 7 2 7 10 6 6 3 5 5 6 9 1 9 3 1 10 3 5 6 8 7 3 7
> cumsum(x2[!is.na(x2)])


## Output

[1] 10 15 19 20 22 25 34 39 47 57 67 70 78 82 90 97 103 111 113
[20] 117 121 130 137 144 153 162 165 172 174 181 191 197 203 206 211 216 222 231
[39] 232 241 244 245 255 258 263 269 277 284 287 294

## Example

Live Demo

> x3<-sample(c(1:10,NA),50,replace=TRUE)
> x3

## Output

[1] 6 5 3 6 9 8 9 10 NA 8 4 10 10 4 4 NA 8 4 6 5 2 10 2 2 4
[26] 10 9 NA NA 5 3 3 6 10 5 5 9 4 7 1 3 4 NA 7 3 6 7 5 8 6
> cumsum(x3[!is.na(x3)])


## Output

[1] 6 11 14 20 29 37 46 56 64 68 78 88 92 96 104 108 114 119 121
[20] 131 133 135 139 149 158 163 166 169 175 185 190 195 204 208 215 216 219 223
[39] 230 233 239 246 251 259 265

## Example

Live Demo

> x4<-sample(c(2,5,10,15,NA),50,replace=TRUE)
> x4

## Output

[1] 2 NA NA 2 NA 2 5 10 NA 5 NA 5 NA NA 15 2 NA NA 10 10 5 NA 15 15 5
[26] NA 15 15 2 10 NA 5 5 10 NA 10 5 10 NA 5 2 2 15 10 10 10 2 5 2 5
> cumsum(x4[!is.na(x4)])


## Output

[1] 2 4 6 11 21 26 31 46 48 58 68 73 88 103 108 123 138 140 150
[20] 155 160 170 180 185 195 200 202 204 219 229 239 249 251 256 258 263

## Example

Live Demo

> x5<-sample(c(sample(c(100:120,NA),1000,replace=TRUE)),50,replace=TRUE)
> x5

## Output

[1] 120 112 117 105 104 NA 107 109 117 114 108 103 117 113 108 110 110 107 118
[20] 101 119 108 108 106 116 120 116 NA 104 107 100 119 102 107 116 119 117 107
[39] 111 109 103 100 116 104 109 108 117 NA NA 114
> cumsum(x5[!is.na(x5)])


## Output

[1] 120 232 349 454 558 665 774 891 1005 1113 1216 1333 1446 1554 1664
[16] 1774 1881 1999 2100 2219 2327 2435 2541 2657 2777 2893 2997 3104 3204 3323
[31] 3425 3532 3648 3767 3884 3991 4102 4211 4314 4414 4530 4634 4743 4851 4968
[46] 5082

## Example

Live Demo

> x6<-sample(c(rnorm(10),NA),50,replace=TRUE)
> x6

## Output

[1] 0.6251684 -0.9355181 -0.5358416 0.3970438 -0.6420775 -0.6420775
[7] 0.7146878 -0.3347836 0.7146878 0.6359577 -0.3347836 1.0612153
[13] 0.7146878 NA 0.3970438 0.6640442 0.6359577 -0.5358416
[19] -0.6420775 NA -0.9355181 0.3970438 0.7146878 0.7146878
[25] NA NA 0.3970438 -0.3347836 0.6640442 -0.9355181
[31] 0.6251684 0.6251684 NA 0.6640442 0.6640442 0.6640442
[37] 0.7146878 0.7146878 -0.9355181 1.0612153 0.6359577 0.6359577
[43] 0.6640442 NA -0.5358416 0.6359577 NA -0.9355181
[49] 0.7146878 1.0612153
> cumsum(x6[!is.na(x6)])


## Output

[1] 0.625168430 -0.310349665 -0.846191269 -0.449147437 -1.091224896
[6] -1.733302356 -1.018614583 -1.353398134 -0.638710361 -0.002752681
[11] -0.337536232 0.723679044 1.438366817 1.835410648 2.499454829
[16] 3.135412509 2.599570905 1.957493446 1.021975351 1.419019183
[21] 2.133706956 2.848394728 3.245438560 2.910655010 3.574699191
[26] 2.639181096 3.264349526 3.889517956 4.553562137 5.217606318
[31] 5.881650499 6.596338271 7.311026044 6.375507949 7.436723225
[36] 8.072680905 8.708638585 9.372682765 8.836841161 9.472798841
[41] 8.537280746 9.251968519 10.313183795

## Example

Live Demo

> x7<-sample(c(rpois(5,2),NA),50,replace=TRUE)
> x7

## Output

[1] 5 2 NA NA 2 5 2 5 2 2 2 5 2 2 2 2 2 NA 5 2 5 5 3 3 2
[26] 5 NA NA 5 5 2 5 5 2 2 NA 5 2 NA 3 2 5 2 2 NA NA 2 NA NA 2
> cumsum(x7[!is.na(x7)])


## Output

[1] 5 7 9 14 16 21 23 25 27 32 34 36 38 40 42 47 49 54 59
[20] 62 65 67 72 77 82 84 89 94 96 98 103 105 108 110 115 117 119 121
[39] 123

## Example

Live Demo

> x8<-sample(c(runif(10,2,5),NA),50,replace=TRUE)
> x8

## Output

[1] NA 4.251011 3.877430 3.144553 3.144553 3.053629 3.053629 4.755601
[9] 3.694577 3.144553 3.877430 4.251011 3.694577 3.694577 2.142446 3.053629
[17] 3.694577 3.634964 NA 3.144553 NA 3.039430 4.251011 4.105852
[25] 4.251011 3.634964 NA 2.142446 4.251011 3.877430 4.105852 3.877430
[33] 4.105852 3.053629 NA 4.105852 3.694577 NA 3.144553 4.755601
[41] 3.053629 3.694577 NA 4.251011 4.251011 4.755601 3.634964 4.755601
[49] 3.694577 3.694577
> cumsum(x8[!is.na(x8)])


## Output

[1] 4.251011 8.128441 11.272994 14.417547 17.471176 20.524805
[7] 25.280406 28.974983 32.119536 35.996966 40.247977 43.942554
[13] 47.637131 49.779577 52.833207 56.527784 60.162748 63.307301
[19] 66.346731 70.597742 74.703594 78.954605 82.589569 84.732016
[25] 88.983027 92.860457 96.966309 100.843739 104.949590 108.003220
[31] 112.109072 115.803649 118.948202 123.703802 126.757432 130.452009
[37] 134.703019 138.954030 143.709631 147.344596 152.100196 155.794773
[43] 159.489350

## Example

Live Demo

> x9<-sample(c(rexp(5,2),NA),50,replace=TRUE)
> x9

## Output

[1] 1.10889027 1.15796798 NA 0.17418154 0.04162214 1.10889027
[7] 0.01356318 0.01356318 0.01356318 1.15796798 0.17418154 0.17418154
[13] 0.04162214 1.15796798 NA 0.17418154 1.10889027 NA
[19] 1.10889027 0.01356318 NA 0.04162214 1.10889027 0.01356318
[25] 0.04162214 0.04162214 1.15796798 1.10889027 0.17418154 NA
[31] 1.15796798 0.04162214 0.01356318 1.15796798 0.17418154 0.04162214
[37] 1.15796798 NA 0.01356318 NA 1.15796798 0.01356318
[43] NA 1.10889027 1.15796798 1.10889027 1.10889027 0.17418154
[49] 0.01356318 1.10889027
> cumsum(x9[!is.na(x9)])


## Output

[1] 1.108890 2.266858 2.441040 2.482662 3.591552 3.605115 3.618679
[8] 3.632242 4.790210 4.964391 5.138573 5.180195 6.338163 6.512344
[15] 7.621235 8.730125 8.743688 8.785310 9.894201 9.907764 9.949386
[22] 9.991008 11.148976 12.257866 12.432048 13.590016 13.631638 13.645201
[29] 14.803169 14.977351 15.018973 16.176941 16.190504 17.348472 17.362035
[36] 18.470925 19.628893 20.737784 21.846674 22.020855 22.034419 23.143309
Updated on 04-Sep-2020 10:31:35