Count and Assign Consecutive Occurrences of Variable

820 views Asked by At

I wish to count consecutive occurrence of any value and assign that count to that value in next column. Below is the example of input and desired output:

dataset <- data.frame(input = c("a","b","b","a","a","c","a","a","a","a","b","c"))
dataset$count <- c(1,2,2,2,2,1,4,4,4,4,1,1)

dataset  
   input   count
     a       1
     b       2
     b       2
     a       2
     a       2
     c       1
     a       4
     a       4
     a       4
     a       4
     b       1
     c       1

With rle(dataset$input) I can just get number of occurrences of each value. But I want resulting output in above format.

My question is similar to: R: count consecutive occurrences of values in a single column But here output is in sequence and I want to assign the count itself to that value.

2

There are 2 answers

0
Ronak Shah On BEST ANSWER

You can repeat the lengths argument lengths time in rle

with(rle(dataset$input), rep(lengths, lengths))
#[1] 1 2 2 2 2 1 4 4 4 4 1 1

Using dplyr, we can use lag to create groups and then count the number of rows in each group.

library(dplyr)

dataset %>%
  group_by(gr = cumsum(input != lag(input, default = first(input)))) %>%
  mutate(count = n())

and with data.table

library(data.table)
setDT(dataset)[, count:= .N, rleid(input)]

data

Make sure the input column is character and not factor.

dataset <- data.frame(input = c("a","b","b","a","a","c","a","a","a","a","b","c"),
           stringsAsFactors = FALSE)
0
akrun On

We can use rleid with dplyr

library(dplyr)
dataset %>%
   group_by(grp = rleid(input)) %>%
   mutate(count = n())