I have a sequence of events encoded as A, B and C. For each element, I need to count how many times this element has been repeated before, but if it does not repeat, the counter should decrease by one for each row. When the first collision of each element, the counter is to zero. For instance:
x<-c('A','A','A','B','C','C','A','B','A','C')
y<-c(0,1,2,0,0,1,-2,-4,-4,-3)
cbind(x,y)
x y
[1,] "A" "0"
[2,] "A" "1"
[3,] "A" "2"
[4,] "B" "0"
[5,] "C" "0"
[6,] "C" "1"
[7,] "A" "-2"
[8,] "B" "-4"
[9,] "A" "-4"
[10,] "C" "-3"
I need to create a column y from x. I know what I can use rlefor the length of the run, but I do not know how to get the time since the last meeting of a particular event to reduce the counter.