Created
June 9, 2016 12:29
-
-
Save phil8192/0675493ff1a92f6f0ce2263f2d6aa1a2 to your computer and use it in GitHub Desktop.
cumulative sequence
This file contains 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
##' 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