Created
October 14, 2014 17:31
-
-
Save dfeng/b76341975fc391ee2a77 to your computer and use it in GitHub Desktop.
Amazing'r
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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