Computes the prior-to (i.e., the cumulative sum prior to but not including the current value) or the reverse (i.e., the number that large or larger) cumulative sum of a vector. Also works for 1-dimensional tables, matrices, and data.frames, though it is best used with vectors.
rcumsum(x) pcumsum(x)
x | a numeric object. |
---|
A numeric vector that contains the prior-to or reverse cumulative sums.
An NA
in the vector causes all returned values at and after the first NA
for pcumsum
and at and before the last NA
for rcumsum
to be NA
. See the examples.
cumsum
.
## Simple example cbind(vals=1:10, cum=cumsum(1:10), pcum=pcumsum(1:10), rcum=rcumsum(1:10))#> vals cum pcum rcum #> [1,] 1 1 0 55 #> [2,] 2 3 1 54 #> [3,] 3 6 3 52 #> [4,] 4 10 6 49 #> [5,] 5 15 10 45 #> [6,] 6 21 15 40 #> [7,] 7 28 21 34 #> [8,] 8 36 28 27 #> [9,] 9 45 36 19 #> [10,] 10 55 45 10## Example with NA vals <- c(1,2,NA,3) cbind(vals, cum=cumsum(vals), pcum=pcumsum(vals), rcum=rcumsum(vals))#> vals cum pcum rcum #> [1,] 1 1 0 NA #> [2,] 2 3 1 NA #> [3,] NA NA NA NA #> [4,] 3 NA NA 3## Example with NA vals <- c(1,2,NA,3,NA,4) cbind(vals, cum=cumsum(vals), pcum=pcumsum(vals), rcum=rcumsum(vals))#> vals cum pcum rcum #> [1,] 1 1 0 NA #> [2,] 2 3 1 NA #> [3,] NA NA NA NA #> [4,] 3 NA NA NA #> [5,] NA NA NA NA #> [6,] 4 NA NA 4## Example with a matrix mat <- matrix(c(1,2,3,4,5),nrow=1) cumsum(mat)#> [1] 1 3 6 10 15pcumsum(mat)#> [,1] [,2] [,3] [,4] [,5] #> [1,] 0 1 3 6 10rcumsum(mat)#> [1] 15 14 12 9 5## Example with a table (must be 1-d) df <- sample(1:10,100,replace=TRUE) tbl <- table(df) cumsum(tbl)#> 1 2 3 4 5 6 7 8 9 10 #> 17 31 42 51 61 66 71 76 86 100pcumsum(tbl)#> df #> 1 2 3 4 5 6 7 8 9 10 #> 0 17 31 42 51 61 66 71 76 86rcumsum(tbl)#> 1 2 3 4 5 6 7 8 9 10 #> 100 83 69 58 49 39 34 29 24 14## Example with a data.frame (must be 1-d) df <- sample(1:10,100,replace=TRUE) tbl <- as.data.frame(table(df))[,-1] cumsum(tbl)#> [1] 6 19 26 33 42 51 67 82 91 100pcumsum(tbl)#> [1] 0 6 19 26 33 42 51 67 82 91rcumsum(tbl)#> [1] 100 94 81 74 67 58 49 33 18 9