torakxkz
torakxkz

Reputation: 493

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

Upvotes: 1

Views: 409

Answers (3)

akrun
akrun

Reputation: 887128

We could use rle with ave in base R

data$Result2 <- with(data, ave(Value, ID, FUN = 
   function(x) inverse.rle(within.list(rle(x), values <- seq_along(values)))))
data$Result2
#[1] 1 1 2 3 4 5 5 1 2 2 3 1 1 1

Upvotes: 1

Karthik S
Karthik S

Reputation: 11584

Does this work:

library(dplyr)
data %>% group_by(ID) %>% mutate(r = rep(seq_along(rle(ID*Value)$values), rle(ID*Value)$lengths))
# A tibble: 14 x 4
# Groups:   ID [3]
      ID Value Result     r
   <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

Upvotes: 1

Matt
Matt

Reputation: 7385

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

Upvotes: 2

Related Questions