Search code examples
rdplyrseqcumsumrle

Rows sequence by group using two columns


Suppose I have the following df

data <- data.frame(ID = c(1,1,1,1,1,1,1,2,2,2,2,3,3,3),
               Value = c(1,1,0,1,0,1,1,1,0,0,1,0,0,0),
               Result = c(1,1,2,3,4,5,5,1,2,2,3,1,1,1))

How can I obtain column Result from the first two columns?

I have tried different approaches using rle, seq, cumsum and cur_group_id but can't get the Result column easily


Solution

  • library(data.table)
    library(dplyr)
    
    data %>% 
      group_by(ID) %>% 
      mutate(Result2 = rleid(Value))
    

    This gives us:

         ID Value Result Result2
       <dbl> <dbl>  <dbl>   <int>
     1     1     1      1       1
     2     1     1      1       1
     3     1     0      2       2
     4     1     1      3       3
     5     1     0      4       4
     6     1     1      5       5
     7     1     1      5       5
     8     2     1      1       1
     9     2     0      2       2
    10     2     0      2       2
    11     2     1      3       3
    12     3     0      1       1
    13     3     0      1       1
    14     3     0      1       1