Skip to content

Instantly share code, notes, and snippets.

@MattSandy
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattSandy/7f73c4868313c2dced38 to your computer and use it in GitHub Desktop.
Save MattSandy/7f73c4868313c2dced38 to your computer and use it in GitHub Desktop.
Find repeating patterns in an R vector from a vector of intervals to check.
vector <- c(10,1,0,10,0,0,10,0,0,10)
matches <- find_patterns(vector,seq(2,3))
find_patterns <- function (vector, intervals) {
matches <- matrix(c(NA, NA), nrow=1, ncol=2)
for(interval in intervals) {
for(i in 1:interval) {
if(sd(vector[seq(i,length(vector),interval)])==0) {
if(is.na(matches[1,1])) {
matches[1,] <- c(vector[i],interval)
} else {
matches <- rbind(matches,c(vector[i],interval))
}
}
}
}
return(matches)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment