I have a vector:
test <-c(1,1,0,2,2,3,4,1,1,0)
test
# [1] 1 1 0 2 2 3 4 1 1 0
I want to construct an grouping variable which indicates when values change:
# [1] 1 1 2 3 3 4 5 6 6 7
What is the best way to do this?
Use run length encoding (rle
), seq_along
and rep
r <- rle(test)
changes <- rep(seq_along(r$lengths), r$lengths)
changes
## [1] 1 1 2 3 3 4 5 6 6 7
Alternative option, which will admittedly only work for numeric data.
test <-c(1,1,0,2,2,3,4,1,1,0)
cumsum(c(1L, diff(test) != 0))
# [1] 1 1 2 3 3 4 5 6 6 7
And a convoluted variation that will work for any data types:
head(cumsum(c(TRUE, c(tail(test, -1), NA) != test)), -1)
# [1] 1 1 2 3 3 4 5 6 6 7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With