问题
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?
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/17539175/create-index-for-contiguous-runs-of-values