How can I create a "group" vector that identifies sequences of same values in another vector.
From this
x <- c(0,1,0,0,1,0,1)
I want to create this
outcome <- c(1,2,3,3,4,5,6)
[1] 0 1 0 0 1 0 1
[1] 1 2 3 3 4 5 6
So, whenever there is a new sequence of the same values there is a new group number (or can be something other than a number as well).
I would actually know ways to get there, but they are all hideous. The best I can come up with is
comparison <- x != lag(x)
cumsum(replace_na(comparison, TRUE))
but like I said - hideous. There must be a better way and I hope someone knows it.