Compute counting variable in dataframe

r

Solution

I would use `rle()` to get the run lengths of `list` and then use the handy `sequence()` function to generate the desired counter from the `$lengths` component returned by `rle()`:

R> sequence(rle(as.character(data$list))$lengths)
 [1] 1 2 3 1 2 3 4 1 2 3 1 2

Notice we have to convert `list` to an atomic vector (character vector in my case) as a factor is not allowed in `rle()`.

To put that into `data`, then wrap this in a call such as

data <- transform(data, b = sequence(rle(as.character(list))$lengths))

which gives

R> data <- transform(data, b = sequence(rle(as.character(list))$lengths))
R> data
    a list b
1   1    x 1
2   2    x 2
3   3    x 3
4   4    y 1
5   5    y 2
6   6    y 3
7   7    y 4
8   8    z 1
9   9    z 2
10 10    z 3
11 11    x 1
12 12    x 2

Problem

I have an R data frame: ``` a <- 1:12 list <- c(rep("x",3),rep("y",4),rep("z",3),rep("x",2)) data <- data.frame(a,list) data a list 1 x 2 x 3 x 4 y 5 y 6 y 7 y 8 z 9 z 10 z 11 x 12 x ``` I want to create a new column which begins counting at 1 every time the value of "list" changes, i.e. in this example: ``` b <- c(1:3,1:4,1:3,1:2) data <- data.frame(a,list,b) ``` I am far from being an expert in R and cannot for the life of me work out an efficient way of doing this. My main problem seems to be that any value of "list" can come back at any time, but there is no rule to the length of the blocks of one value. Does anyone have any ideas? Thanks!

Original source