Search code examples
rdplyr

Replace all values in column after occurrence of specific value


This is probably simple, but I'm missing it. In the example I have several id's with multiple values each. Within each id, I want to be able to set x to equal 0 only after the occurrence of a 2, leaving the other values untouched.

Is there a dplyr way to do this?

test <- structure(list(id = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 
                              3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 
                              8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10), x = c(0L, 1L, 1L, 
                                                                              2L, 1L, 0L, 0L, 1L, 2L, 1L, 0L, 0L, 1L, 2L, 1L, 0L, 0L, 0L, 0L, 
                                                                              1L, 1L, 2L, 1L, 0L, 0L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 
                                                                              1L, 0L, 0L, 2L, 1L, 1L, 0L, 1L, 2L, 1L, 1L)), row.names = c(NA, 
                                                                                                                                          -46L), class = "data.frame")

> head(test, 10)
   id x
1   1 0
2   1 1
3   1 1
4   1 2
5   1 1
6   2 0
7   2 0
8   2 1
9   2 2
10  2 1

Solution

  • mutate(test, x_new=ifelse(+cumsum(lag(x, default=0)==2), 0, x), .by=id)
    
       id x x_new
    1   1 0     0
    2   1 1     1
    3   1 1     1
    4   1 2     2
    5   1 1     0
    6   2 0     0
    7   2 0     0
    8   2 1     1
    9   2 2     2
    10  2 1     0
    11  3 0     0
    12  3 0     0
    13  3 1     1
    14  3 2     2
    15  3 1     0