Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create group number for contiguous runs of equal values

Is there is a faster way to make a counter index than using a loop? For each contiguous run of equal values, the index should be the same. I find the looping very slow especially when the data is so big.

For illustration, here is the input and desired output

x <- c(2, 3, 9, 2, 4, 4, 3, 4, 4, 5, 5, 5, 1)

Desired resulting counter:

c(1, 2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8, 9)

Note that non-contiguous runs have different indexes. E.g. see the desired indexes of the values 2 and 4

My inefficient code is this:

group[1]<-1
counter<-1
for (i in 2:n){
if (x[i]==x[i-1]){
    group[i]<-counter
}else{
    counter<-counter+1
    group[1]<-counter}
}
like image 703
Rens Avatar asked May 19 '15 00:05

Rens


2 Answers

Above answer by Jota can be further simplified to, which will be even faster

with(rle(x), rep(1:length(lengths), lengths))

 [1] 1 2 3 4 5 5 6 7 7 8 8 8 9
like image 127
AnilGoyal Avatar answered Sep 30 '22 09:09

AnilGoyal


If you have numeric values like this, you can use diff and cumsum to add up changes in values

x <- c(2,3,9,2,4,4,3,4,4,5,5,5,1)
cumsum(c(1,diff(x)!=0))
# [1] 1 2 3 4 5 5 6 7 7 8 8 8 9
like image 27
MrFlick Avatar answered Sep 30 '22 11:09

MrFlick