R exact selection based on %in%
One dplyr
possibility could be:
df %>%
group_by(id) %>%
mutate(tag = all(c(-1, -2, -3, -4) %in% time) * 1)
id time tag
<int> <int> <dbl>
1 1 -4 1
2 1 -3 1
3 1 -2 1
4 1 -1 1
5 2 -1 0
6 2 -2 0
7 3 -1 0
8 3 -3 0
9 4 -1 1
10 4 -2 1
11 4 -3 1
12 4 -4 1
And the same with base R
could be:
with(df, ave(time, id, FUN = function(x) all(c(-1, -2, -3, -4) %in% x) * 1))