I have a sequence of dates (years) that is irregular.
Specifically, year 2004 is followed by 2005, 2006 is missing, 2007 is present, followed by 2008, then sequence is missing years until 2014.
# data input
df_in <-
data.frame(seq = c(2004L, 2005L, 2007L, 2008L, 2014L, 2015L, 2016L))
# desired result
df_out <-
data.frame(df_in, grp = c(1L, 1L, 2L, 2L, 3L, 3L, 3L))
seq grp
1 2004 1
2 2005 1
3 2007 2
4 2008 2
5 2014 3
6 2015 3
7 2016 3
I would like to find a way to generate groups of years that are next to each other. So, group 1 would contain years 2004 and 2005, group 2 years 2007 and 2008, and group 3 years from 2014 to 2016.
Any help would be appreciated.