I am using slider::slider_index() to set up a rolling time windows over a dataset. The idea is to apply a function to all points belonging to the time window.
Here is a reproducible example to get all individuals (1 individual = 1 row of the dataframe) belonging to 2 consecutive days.
# packages
library(dplyr)
library(lubridate)
library(slider)
# dummy data
company <- tibble(
sales = c(2, 3, 1, 8, 4, 6, 2),
index = as.Date("2019-08-29") + c(0, 0, 1, 1, 1, 5, 6),
)
# what I have done so far
roll_index <- slide_index(company, company$index, ~ .x, .before = lubridate::days(1))
Which gives:
> roll_index
[[1]]
# A tibble: 2 x 2
sales index
<dbl> <date>
1 2 2019-08-29
2 3 2019-08-29
[[2]]
# A tibble: 2 x 2
sales index
<dbl> <date>
1 2 2019-08-29
2 3 2019-08-29
[[3]]
# A tibble: 5 x 2
sales index
<dbl> <date>
1 2 2019-08-29
2 3 2019-08-29
3 1 2019-08-30
4 8 2019-08-30
5 4 2019-08-30
[[4]]
# A tibble: 5 x 2
sales index
<dbl> <date>
1 2 2019-08-29
2 3 2019-08-29
3 1 2019-08-30
4 8 2019-08-30
5 4 2019-08-30
[[5]]
# A tibble: 5 x 2
sales index
<dbl> <date>
1 2 2019-08-29
2 3 2019-08-29
3 1 2019-08-30
4 8 2019-08-30
5 4 2019-08-30
[[6]]
# A tibble: 1 x 2
sales index
<dbl> <date>
1 6 2019-09-03
[[7]]
# A tibble: 2 x 2
sales index
<dbl> <date>
1 6 2019-09-03
2 2 2019-09-04
So far so good. The problem is that it is not exactly what I expected. As you can see, some data are duplicated: roll_index[[3]], roll_index[[4]] and roll_index[[5]] are the same.
What I understand is that slider::slide_index() goes one individual after the other even if multiple individuals belong to the same index value.
How can I avoid this data duplicates as they tend to increase quite a lot the size of the ouput when I use my actual data?