I'm having a play with R, and am struggling to get to grips with the different programming style required.
The task I'm trying to do, is given a sequence of numbers e.g. (1,2,3,3,3,4,5,5,1), to work out at each point the number of consecutive previous points with the same value. E.g. the answer for this example would be: (0,0,0,1,2,0,0,1,0).
In a more conventional programming language e.g. Python I'd do something like this:
flat_count = 0
for i in range(1, len(seq)):
if seq[i] == seq[i-1]:
flat_count++
else:
flat_count = 0
seq[i] = flat_count
seq[0] = 0
Since my impression is that for loops in R should be avoided at all costs, I'm a bit confused as to where to begin.
My best attempt so far, is as follows:
runs <- rle(seq)
seqs <- sapply(runs$lengths, FUN=seq)
I'm not sure if this is a particular efficient way however, and if it is, I'm not sure how to concatenate my resulting lists in seqs together.
Any help appreciated, or just general best practices for R.
Thanks