On the sample data.frame:
df <- data.frame(V1 = c(1, 3, 4, NA, NA, 6, 9, NA, 10),
V2 = seq(1:9))
Using group_by() in the fashion of filter() gives these results:
df %>%
group_by(miss = !is.na(V1)) %>%
mutate(lag = V1 - lag(V1))
# A tibble: 9 x 4
# Groups: miss [2]
V1 V2 miss lag
<dbl> <dbl> <lgl> <dbl>
1 1. 1. TRUE NA
2 3. 2. TRUE 2.
3 4. 3. TRUE 1.
4 NA 4. FALSE NA
5 NA 5. FALSE NA
6 6. 6. TRUE 2.
7 9. 7. TRUE 3.
8 NA 8. FALSE NA
9 10. 9. TRUE 1.
It is exactly what I wanted but I'm curious whether it is intended to use group_by() also this way.