Suppose I am having the following data set
data
Group Date
A 2016-03-10
A 2016-03-11
A 2016-03-12
A 2016-04-13
A 2016-04-14
A 2016-05-15
A 2016-05-16
A 2016-05-17
B 2016-02-11
B 2016-02-12
B 2016-02-13
B 2016-02-19
B 2016-03-15
I want to find the different date intervals for each group. For example, for Group A, 2016-03-10 to 2016-03-12 should be interval 1, 2016-04-13 to 2016-04-14 should be interval 2 and 2016-05-15 to 2016-05-17 should be interval 3. I want to find where all there have been breaks and how many breaks have occurred for each group. In this way I can analyse that. This should be computed for each group. The following should be my ideal output,
Group Date Interval
A 2016-03-10 1
A 2016-03-11 1
A 2016-03-12 1
A 2016-04-13 2
A 2016-04-14 2
A 2016-05-15 3
A 2016-05-16 3
A 2016-05-17 3
B 2016-02-11 1
B 2016-02-12 1
B 2016-02-13 1
B 2016-02-19 2
B 2016-03-15 3
The following are my tryings,
data %>% group_by(Group) %>% mutate(Date - lag(Date)) .
This gives my output of NAs for first row, 1 whenever the date changes and 0 when it doesnt change. But I want something like 1,2,3 for each date interval.
Updated dataset for which it is not working,
group date count
(factor) (date)
1 Albany 2016-02-15 55
2 Albany 2016-02-16 1
3 Albany 2016-04-08 40