hutilscpp (version 0.10.4)

cumsum_reset: Cumulative sum unless reset

Description

Cumulative sum unless reset

Usage

cumsum_reset(x, y = as.integer(x))

Value

A vector of cumulative sums, resetting whenever x is FALSE. The return type is double if y is double; otherwise an integer vector. Integer overflow wraps around, rather than being promoted to double type, as this function is intended for 'shortish' runs of cumulative sums.

If length(x) == 0, y is returned (i.e. integer(0) or double(0).

Arguments

x

A logical vector indicating when the sum should continue. Missing values in x is an error.

y

Optional: a numeric vector the same length as x to cumulatively sum.

Examples

Run this code
cumsum_reset(c(TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE))
cumsum_reset(c(TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE),
             c(1000, 1000, 10000,   10,   20,   33,     0))

Run the code above in your browser using DataLab