Skip to content

Instantly share code, notes, and snippets.

@wush978
Created February 27, 2013 03:42
Show Gist options
  • Save wush978/5044851 to your computer and use it in GitHub Desktop.
Save wush978/5044851 to your computer and use it in GitHub Desktop.
detect_continuous <- function(src, gap = 1) {
index.bool <- diff(src) == gap
is.in_sequence <- FALSE
start <- c()
end <- c()
for(i in 1:length(index.bool)) {
if(!index.bool[i]) {
is.in_sequence <- FALSE
}
if(index.bool[i] & !is.in_sequence) {
start[length(start) + 1] <- i
end[length(end) + 1] <- i+1
is.in_sequence <- TRUE
next
}
if(index.bool[i] & is.in_sequence) {
end[length(end)] <- end[length(end)] + 1
next
}
}
return(data.frame(start=start, end=end))
}
src <- c(2:6, 3:5, 1:10)
detect_continuous(src)
src[1:5]
src[6:8]
src[9:18]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment