Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create counter within consecutive runs of values

Tags:

I wish to create a sequential number within each run of equal values, like a counter of occurrences, which restarts once the value in the current row is different from the previous row.

Please find an example of input and expected output below.

dataset <- data.frame(input = c("a","b","b","a","a","c","a","a","a","a","b","c")) dataset$counter <- c(1,1,2,1,2,1,1,2,3,4,1,1) dataset  #    input counter # 1      a       1 # 2      b       1 # 3      b       2 # 4      a       1 # 5      a       2 # 6      c       1 # 7      a       1 # 8      a       2 # 9      a       3 # 10     a       4 # 11     b       1 # 12     c       1 

My question is very similar to this one: Cumulative sequence of occurrences of values.

like image 737
Richard Avatar asked Nov 15 '13 10:11

Richard


People also ask

How do you count consecutive occurrences in R?

A: Use the rle() function. For example, let's apply rle() to the following sequence of numbers. We see that rle() returns a list of two elements: lengths and values, where the latter gives the unique number of each run, and the former gives the run length, i.e. the number of consecutive repeats within each run.


1 Answers

You need to use sequence and rle:

> sequence(rle(as.character(dataset$input))$lengths)  [1] 1 1 2 1 2 1 1 2 3 4 1 1 
like image 117
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 11 '22 00:10

A5C1D2H2I1M1N2O1R2T1