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

View source: R/FSAUtils.R

rcumsumR Documentation

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

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

rcumsum(x)

pcumsum(x)

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, DerekOgle51@gmail.com

See Also

cumsum.

Examples

## 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)

FSA documentation built on Aug. 27, 2023, 1:06 a.m.

Related to rcumsum in FSA...