Skip to content

Instantly share code, notes, and snippets.

@dfeng
Created October 14, 2014 17:31
Show Gist options
  • Select an option

  • Save dfeng/b76341975fc391ee2a77 to your computer and use it in GitHub Desktop.

Select an option

Save dfeng/b76341975fc391ee2a77 to your computer and use it in GitHub Desktop.
Amazing'r
# Source: http://stackoverflow.com/questions/11671731/in-r-how-can-i-make-a-running-count-of-runs
x <- data.frame(end.group=c(0,0,1,0,0,1,1,0,0,0,1,1,1,0,1))
# create groups
x$group <- rev(cumsum(rev(x$end.group)))
# re-number groups from smallest to largest
x$group <- abs(x$group-max(x$group)-1)
x$group.count <- ave(x$end.group, x$group, FUN=seq_along)
# Even Better: http://stackoverflow.com/questions/9961700/how-to-partition-when-ranking-on-a-particular-column
DT[, list(Value, Order=seq_len(.N)), by=ID]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment