Search code examples
rdplyrtidyverse

Conditional replacing of a numeric value in dplyr


Dear all I have a data frame that looks like this

df <- data.frame(time=c(1,2,3,4,1,2,3,4,5), type=c("A","A","A","A","B","B","B","B","B"), count=c(10,0,0,1,8,0,1,0,1))
df

 time type count
1    1    A    10
2    2    A     0
3    3    A     0
4    4    A     1
5    1    B     8
6    2    B     0
7    3    B     1
8    4    B     0
9    5    B     1

I want to examine each group of types and if I see that one count is 0 then to replace the next count forward in time with 0. I do not count to be resurrected from the zero.

I want my data to looks like this

 time type count
1    1    A    10
2    2    A     0
3    3    A     0
4    4    A     0
5    1    B     8
6    2    B     0
7    3    B     0
8    4    B     0
9    5    B     0

Solution

  • If I understood correctly

    library(tidyverse)
    df <-
      data.frame(
        time = c(1, 2, 3, 4, 1, 2, 3, 4, 5),
        type = c("A", "A", "A", "A", "B", "B", "B", "B", "B"),
        count = c(10, 0, 0, 1, 8, 0, 1, 0, 1)
      )
    
    df %>% 
      group_by(type) %>% 
      mutate(count = if_else(lag(count, default = first(count)) == 0, 0, count))
    #> # A tibble: 9 x 3
    #> # Groups:   type [2]
    #>    time type  count
    #>   <dbl> <chr> <dbl>
    #> 1     1 A        10
    #> 2     2 A         0
    #> 3     3 A         0
    #> 4     4 A         0
    #> 5     1 B         8
    #> 6     2 B         0
    #> 7     3 B         0
    #> 8     4 B         0
    #> 9     5 B         0
    

    Created on 2021-09-10 by the reprex package (v2.0.1)