Last active
August 29, 2015 14:24
-
-
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.
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
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