Here is my dataframe:
df <- data.frame(col_1 = c('11/13/2007', '11/17/2007', '11/19/2007', '11/25/2007', '11/28/2007'),
col_2 = c('A', 'B', 'C', 'D', 'E'))
I would like to add column, which would group elements using time difference of dates in col_1
. For example first, second and third rows will be in group 1, since dates differ only by less than 5 days (between each consecutive dates) and row four and five will be in group 2. We will get two groups since two consecutive dates '11/19/2007' and '11/25/2007' differ by more than 5 days.
I can compute day difference between dates, but now sure how to create grouping. I would prefer solution with dplyr
, but any piece of advice is appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…