rcumsum | R Documentation |
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.
Derek H. Ogle, DerekOgle51@gmail.com
cumsum
.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.