Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create index for contiguous runs of values

Tags:

r

sequence

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?

like image 481
Seen Avatar asked Jul 09 '13 02:07

Seen


2 Answers

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
like image 53
mnel Avatar answered Sep 24 '22 14:09

mnel


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
like image 22
thelatemail Avatar answered Sep 23 '22 14:09

thelatemail