'Add 1 to column for any value match by group

I would like to add 1 to the TIMECYCLE column if there is a 0 present in a particular group (ie, if any value within a given ID is 0). I'm not sure what is missing, but I feel like it is something simple. Reprex and current code with accompanying error message below.

Reprex:

reprex = structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), TIMEHR = c(0, 0.5, 1, 2, 4, 0, 0.5, 1, 2, 4, 4, 0, 0, 0.5, 
1, 2, 4, 6, 8, 0, 0.5, 1, 2, 4, 6, 8, 2), TIMEDAY = c(8, 8, 8, 
8, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 8, 8, 8, 8, 8, 
8, 8, 8), TIMECYCLE = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 0, 
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, 
-27L), class = c("tbl_df", "tbl", "data.frame"))

Reprex print:

      ID TIMEHR TIMEDAY TIMECYCLE
   <int>  <dbl>   <dbl>     <dbl>
 1     1    0         8         1
 2     1    0.5       8         1
 3     1    1         8         1
 4     1    2         8         1
 5     1    4         8         1
 6     1    0         1         1
 7     1    0.5       1         1
 8     1    1         1         1
 9     1    2         1         1
10     1    4         1         1
11     1    4         1         2
12     1    0         1         2
13     2    0         1         0
14     2    0.5       1         0
15     2    1         1         0
16     2    2         1         0
17     2    4         1         0
18     2    6         1         0
19     2    8         1         0
20     2    0         8         1
21     2    0.5       8         1
22     2    1         8         1
23     2    2         8         1
24     2    4         8         1
25     2    6         8         1
26     2    8         8         1
27     2    2         8         1

Current code:

 reprex %>%
  group_by(ID) %>%
    mutate(TIMECYCLE = if_else(any(TIMECYCLE == 0), TIMECYCLE+1, TIMECYCLE))

Error code:

Error: Problem with `mutate()` column `TIMECYCLE`.
i `TIMECYCLE = if_else(any(TIMECYCLE == 0), TIMECYCLE + 1, TIMECYCLE)`.
x `true` must be length 1 (length of `condition`), not 20.
i The error occurred in group 1: USUBJID = "001-802-008".
Run `rlang::last_error()` to see where the error occurred.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source