I am trying to do analysis on the repeat purchase behavior of the customers.
My goal is two-fold:
- Eliminate all the customer item zip combination that occurs only once (eliminate the non-repeating ones)
- For the records that repeat, I want to summarize the same where I need to get the average number of days between multiple orders and also the total sales
If I have the following data:
Customer# Item Zip Date Qty Net Sales
--------- -------- ----- ---------- --- ---------
ABC123 GHTH123 76137 2014-01-01 10 1500
XYZ999 ZZZZZZZ 68106 2015-02-01 1 50
DEF456 167AAAA 60018 2015-03-01 12 650
XYZ999 YYYYYYY 68106 2015-01-01 3 150
XYZ999 ZZZZZZZ 68106 2015-04-01 10 500
XYZ999 YYYYYYY 68106 2015-03-01 12 600
XYZ999 YYYYYYY 68106 2015-05-01 10 500
ABC123 GHTH123 76137 2014-01-15 8 1200
ABC234 N867689 23218 2014-01-01 10 1500
ABC123 DDFF121 76137 2014-01-27 15 2250
I am trying to get the following output:
Customer# Item Zip Avg Days/Ord Tot Ord Total Amt
--------- -------- ----- ---------- ------- ---------
ABC123 GHTH123 76137 15 2 2700
XYZ999 ZZZZZZZ 68106 60 2 550
XYZ999 YYYYYYY 68106 60 3 1250
I was trying to use the sqldiff package and use DATEDIFF
function but I am not getting anywhere with that as DATEDIFF
doesnt work for R
.
Can someone help me with a better approach here?