Skip to content

Instantly share code, notes, and snippets.

@phil8192
Created June 9, 2016 12:29
Show Gist options
  • Save phil8192/0675493ff1a92f6f0ce2263f2d6aa1a2 to your computer and use it in GitHub Desktop.
Save phil8192/0675493ff1a92f6f0ce2263f2d6aa1a2 to your computer and use it in GitHub Desktop.
cumulative sequence
##' cumulative sequence of sequence changes.
##'
##' assigns a cumulative sum to a sequential occurrence in a vector.
##'
##' @param x a vector containing sequences.
##' @return a vector containing change counts.
##' @author phil
##' @examples
##'
##' my.seq <- c(6, 6, 6, 20, 20, 5, 3, 3, 3, 3, 6, 6)
##' stopifnot(cumseq(my.seq) == c(1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 5, 5))
cumseq <- function(x) {
rle.x <- rle(x)
rle.x$values <- 1:length(rle.x$values)
inverse.rle(rle.x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment