I'm trying to solve a problem of grouping dates together based on certain length of time from a starting date. For example, given the following data:
library(dplyr)
test_df <- tibble::tribble(
~id, ~date,
1,'01/01/2011',
2,'01/05/2011',
3,'01/10/2011',
4,'02/01/2011',
5,'02/10/2011',
6,'02/11/2011',
7,'02/19/2011',
8,'03/01/2011',
9,'03/02/2011',
10,'03/03/2011',
11,'03/04/2011',
12,'04/29/2011',
13,'05/01/2011',
14,'05/02/2011',
15,'05/29/2011',
16,'05/30/2011',
17,'06/01/2011',
18,'06/02/2011',
19,'06/03/2011',
20,'06/10/2011'
) %>%
mutate(date = lubridate::mdy(date))
the goal would be to group all dates within 28 days (this could be varied) from the minimum date. That is, at first pass, the starting date would be 01/01/2011
and we would group all dates 28 days out. Then, we would start over at the next non-grouped date which would be 02/01/2011
and so on until all the dates were grouped.
I realize this can be solved pretty easy with a for loop, but I'm wondering if anyone can think of any simple / creative solutions to solve this in a more tidyverse / functional way.
Thanks!