Create index for contiguous runs of values

瘦欲@ 提交于 2020-08-17 05:24:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!