A periodic sequence is a sequence that repeats itself after n terms, for example, the following is a periodic sequence:
1, 2, 3, 1, 2, 3, 1, 2, 3, …
And we define the period of that sequence to be the number of terms in each subsequence (the subsequence above is 1, 2, 3). So the period for the above sequence is 3.
In R, I can define the above sequence (albeit not to infinity), using:
sequence <- rep(c(1,2,3),n) #n is a predefined variable
So if n = 50, sequence will be the sequence 1, 2, 3, 1, 2, 3, … , 1, 2, 3, where each number has appeared 50 times, in the obvious way.
I am looking to build a function that calculates the periodicity of sequence. Pseudocode is as follows:
period <- function(sequence){
subsequence <- subsequence(sequence) #identify the subsequence
len.subsequence <- length(subsequence) #calculate its length
return(len.subsequence) #return it
}
How would I identify the subsequence? This is sort of a reversing of the rep function, such that I pass in a sequence and it passes out the length of the initial vector.
If the period is always the same, i.e. the sequence never changes, then you could use a loop over
lagto see when a match occurs.With total bias, I would also recommend using
seqle(guess who wrote that function 🙂 ), which is likerlebut finds sequences. detect intervals of the consequent integer sequencesI’m not the only person to edit the source for “rle” that way.