Skip to content

Instantly share code, notes, and snippets.

@zmjones
zmjones / decay.R
Last active October 12, 2015 00:27
time since event and decaying cumulative sums for binary time series
decay <- function(yvar, d) {
# yvar: a binary variable
# d: number of periods
# returns: the cumulative sum of yvar at each point
# where each addition to the count decays away after d periods
yvar[is.na(yvar)] <- 0
run <- cumsum(yvar)
tvar <- seq_along(yvar)
run <- 0
sum <- 0