Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save infotroph/d4dfa87e3692c45b43dd to your computer and use it in GitHub Desktop.
Save infotroph/d4dfa87e3692c45b43dd to your computer and use it in GitHub Desktop.
dat = data.frame(
userID=c("one", "two", "three", "four", "five"),
start_date=as.Date(c("2015-09-01", "2015-09-02", "2015-09-02", "2015-09-03", "2015-09-03")),
end_date=as.Date(c("2015-09-02", NA, "2015-09-03", NA, "2015-09-03")))
n_start = 100 # number of active users on day zero
days = as.Date("2015-08-25")+1:10
n_new = sapply(days, function(x)length(which(dat$start_date == x)))
n_lost = sapply(days, function(x)length(which(dat$end_date == x)))
dn = n_new - n_lost
n_active = cumsum(c(n_start, dn))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment