Hello,
I am struggling to develop a code that counts the number of times a given product has been purchased in the last 3 months not including the month it is in.
Here is a sample of the data:
data =
Col # Date Yr/Month Product
1 1/11/2018 2018/01 A
2 1/15/2018 2018/01 B
3 1/17/2018 2018/01 B
4 1/17/2018 2018/01 C
5 2/20/2018 2018/02 A
6 2/20/2018 2018/02 A
7 2/21/2018 2018/02 A
8 2/26/2018 2018/02 A
9 2/27/2018 2018/02 C
10 3/27/2018 2018/03 C
11 5/10/2018 2018/05 C
12 5/21/2018 2018/05 C
13 6/26/2018 2018/06 C
14 7/9/2018 2018/07 B
15 7/20/2018 2018/07 A
16 8/3/2018 2018/08 C
17 8/3/2018 2018/08 C
18 8/3/2018 2018/08 A
19 8/9/2018 2018/08 B
20 8/9/2018 2018/08 B
21 8/29/2018 2018/08 B
22 9/10/2018 2018/09 C
23 10/3/2018 2018/10 A
24 10/3/2018 2018/10 A
25 10/6/2018 2018/10 A
26 10/6/2018 2018/10 B
27 10/12/2018 2018/10 A
28 10/12/2018 2018/10 B
29 10/25/2018 2018/10 C
30 10/29/2018 2018/10 C
31 11/12/2018 2018/11 A
32 12/19/2018 2018/12 B
33 12/19/2018 2018/12 C
For example, looking at column number 16:
Product C was bought in August of 2018 in both column 16 and column 17. Counting the number of times it was purchased in this month for both column 16 and 17 should be equal to 2.
I can code that part using dplyr:
data1 <- data %>%
group_by(Yr/Month,Product) %>%
summarise(Products = n())
This outputs just the year and month and the number of times a given product is purchased within that month.
Now I want to be able to find how many times product C was purchased between May and July (the 3 months prior to August but not including August).
The result for Col 16 should be 3; since it was purchased twice in May (col 11 and 12) and once in June (col 13) and it wasn't purchased at all in July.
Can anyone point me to a package/code that I would need in order to perform this analysis?
Thank you!