rcumsum: Computes the prior to or reverse cumulative sum of a vector.

Description Usage Arguments Value Note Author(s) See Also Examples

View source: R/FSAUtils.R

Description

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.

Usage

1
2
3

Arguments

x

a numeric object.

Value

A numeric vector that contains the prior-to or reverse cumulative sums.

Note

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.

Author(s)

Derek H. Ogle, derek@derekogle.com

See Also

cumsum.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
## Simple example
cbind(vals=1:10,
      cum=cumsum(1:10),
      pcum=pcumsum(1:10),
      rcum=rcumsum(1:10))

## Example with NA
vals <- c(1,2,NA,3)
cbind(vals,
      cum=cumsum(vals),
      pcum=pcumsum(vals),
      rcum=rcumsum(vals))

## Example with NA
vals <- c(1,2,NA,3,NA,4)
cbind(vals,
      cum=cumsum(vals),
      pcum=pcumsum(vals),
      rcum=rcumsum(vals))
      
## Example with a matrix
mat <- matrix(c(1,2,3,4,5),nrow=1)
cumsum(mat)
pcumsum(mat)
rcumsum(mat)

## Example with a table (must be 1-d)
df <- sample(1:10,100,replace=TRUE)
tbl <- table(df)
cumsum(tbl)
pcumsum(tbl)
rcumsum(tbl)

## 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)
pcumsum(tbl)
rcumsum(tbl)

Example output

## FSA v0.8.16. See citation('FSA') if used in publication.
## Run fishR() for related website and fishR('IFAR') for related book.
      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
     vals cum pcum rcum
[1,]    1   1    0   NA
[2,]    2   3    1   NA
[3,]   NA  NA   NA   NA
[4,]    3  NA   NA    3
     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
[1]  1  3  6 10 15
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    1    3    6   10
[1] 15 14 12  9  5
  1   2   3   4   5   6   7   8   9  10 
 10  18  31  40  51  62  72  82  93 100 
df
 1  2  3  4  5  6  7  8  9 10 
 0 10 18 31 40 51 62 72 82 93 
  1   2   3   4   5   6   7   8   9  10 
100  90  82  69  60  49  38  28  18   7 
 [1]   7  20  29  36  48  60  70  79  84 100
 [1]  0  7 20 29 36 48 60 70 79 84
 [1] 100  93  80  71  64  52  40  30  21  16

FSA documentation built on July 17, 2021, 5:07 p.m.

Related to rcumsum in FSA...