I have a tricky problem I'm trying to solve:
I have data that looks like the following sample:
UniqueID Month
ABC123 1
ABC123 2
ABC123 3
ABC123 4
ABC123 6
ABC123 7
DEF456 3
DEF456 4
DEF456 10
DEF456 11
DEF456 12
DEF456 14
GHI789 2
GHI789 3
JKL012 12
JKL012 13
JKL012 14
The UniqueID is unique per month. The month column refers to a particular month. For example: 1=October 2018, 2=November 2019, and so on. We have a total of 14 different months for which we have data. I want to cumulatively count the number of times we skip a month and when the final month per UniqueID is not 14. The starting month does not factor into the calculation. The resulting calculation would result in the following sample:
UniqueID Month CountSkip
ABC123 1 0
ABC123 2 0
ABC123 3 0
ABC123 4 0
ABC123 6 1
ABC123 7 2
DEF456 3 0
DEF456 4 0
DEF456 10 1
DEF456 11 1
DEF456 12 1
DEF456 14 2
GHI789 2 0
GHI789 3 1
JKL012 12 0
JKL012 13 0
JKL012 14 0
I have a snippet to calculate the total number of skips by doing the following:
data %>%
group_by(UniqueID) %>%
mutate(Skipped = sum(diff(Month) > 1))
How could I modify this to cumulatively count the skips and also account for the last month value not being 14?
Any help would be appreciated! Thank you!