Home > OS >  Count previous column values based on condition in R
Count previous column values based on condition in R

Time:07-20

I have a dataset

df <- structure(list(ID = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
4L, 4L, 4L), clicks = c(0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
1L, 0L, 0L)), class = "data.frame", row.names = c(NA, -13L))

And I would like to count the previous times when there were no clicks by a specific ID group. I have used this code

df %>% 
  as_tibble() %>% 
  mutate(click_reverse = ifelse(clicks==0, 1, 0)) %>% 
  group_by(ID) %>% 
  mutate(noclicks_count = ifelse(click_reverse==1, cumsum(click_reverse),0))

But ideally, I would like to start the count over again when the ID has had a click. So the code doesn't work fully for ID 2 and the desired output should be like this:

      ID clicks click_reverse noclicks_count
   <int>  <int>         <dbl>          <dbl>
 1     1      0             1              1
 2     1      0             1              2
 3     1      1             0              0
 4     2      0             1              1
 5     2      1             0              0
 6     2      0             1              1
 7     2      0             1              2
 8     3      0             1              1
 9     3      0             1              2
10     3      0             1              3
11     4      1             0              0
12     4      0             1              1
13     4      0             1              2

Any ideas/suggestions on how should I approach this problem?

CodePudding user response:

You may try

library(dplyr)
library(data.table)

df %>%
  mutate(clicks_reverse = abs(1-clicks),
         i = data.table::rleid(clicks_reverse)) %>%
  group_by(ID, i) %>%
  mutate(noclicis_count = cumsum(clicks_reverse)) %>%
  ungroup %>%
  select(-i)

      ID clicks clicks_reverse noclicis_count
   <int>  <int>          <dbl>          <dbl>
 1     1      0              1              1
 2     1      0              1              2
 3     1      1              0              0
 4     2      0              1              1
 5     2      1              0              0
 6     2      0              1              1
 7     2      0              1              2
 8     3      0              1              1
 9     3      0              1              2
10     3      0              1              3
11     4      1              0              0
12     4      0              1              1
13     4      0              1              2
  • Related