I am trying to summarise data on a rolling basis depending on what I input.
For example, I could have the following input grouping_value <- 7
which I would then hope to have the earliest Date
in my data frame group by. So if my earliest Date
is 2021-01-01
then I would hope that a new column would be mutated like Group_2021-01-01_to_Group_2021-01-08
and the next group would be Group_2021-01-09_to_Group_2021-01-16
and so on. It would group according to the grouping_value
.
Raw data:
ID | Date | Sales |
---|---|---|
1 | 2021-01-01 | 22 |
1 | 2022-01-03 | 32 |
1 | 2021-01-11 | 42 |
1 | 2022-01-16 | 32 |
1 | 2022-01-13 | 27 |
2 | 2022-01-12 | 42 |
grouping_value <- 7
This grouping_value
would take the earliest date of 2021-01-01
and add 7 to get the group until it has cycled through the whole dataframe. I could also change the group_value <- 30
to group according to 30 days theoretically .
earliest_date_in_data <-
data%>%
summarise(min_Date = min(Date, na.rm = TRUE))
Group Added:
ID | Date | Sales | Group |
---|---|---|---|
1 | 2021-01-01 | 22 | Group_2021-01-01_to_Group_2021-01-08 |
1 | 2021-01-03 | 32 | Group_2021-01-01_to_Group_2021-01-08 |
1 | 2021-01-11 | 42 | Group_2021-01-09_to_Group_2021-01-16 |
1 | 2021-01-16 | 32 | Group_2021-01-09_to_Group_2021-01-16 |
1 | 2021-01-13 | 27 | Group_2021-01-09_to_Group_2021-01-16 |
2 | 2021-01-12 | 48 | Group_2021-01-09_to_Group_2021-01-16 |
I could then group_by
this Group.
The only thing that has occurred to me is to do a case_when()
to group by dates but that would lack the dynamic element. I'd appreciate any help or ideas. Thank you