I have data where I am trying to calculate average sales of last 3 months from most recent order date and also want to calculate YTD sales of that customer and everything groupby customerID.In Orderstatus I only want to calculate Shipped and partially shipped orders.
My data looks like this:
OrderStatus OrderDate OrderTotal CustomerID
Partially Shipped 7/5/2019 229.5 1
Shipped 3/5/2019 48.35 1
Shipped 3/6/2019 31.94 2
Shipped 3/8/2019 91.8 1
Shipped 3/8/2019 45.9 1
Shipped 3/11/2019 98 2
Refunded 6/13/2019 22599 7
Shipped 1/14/2019 0 1
Shipped 5/15/2019 7 7
Shipped 5/15/2019 48.5 7
Shipped 12/15/2018 272.01 1
Shipped 11/15/2018 34.97 1
Shipped 11/1/2018 13.43 1
Shipped 3/15/2018 34.97 1
Shipped 4/15/2019 92.94 7
Shipped 3/15/2018 0 1
Shipped 3/17/2019 102.85 7
Shipped 3/18/2019 49 7
Shipped 3/18/2019 119.95 7
Shipped 3/18/2019 58.96 7
Cancelled 3/19/2018 20.48 1
Shipped 3/20/2019 14.6 7
I have tried aggregating it by customerid but in orderstatus i only want the records where status is shipped. I am not able to put that filter on and also dont know how to take only 3 months for average.
I tried this code:
x <- aggregate(x$Order.Total..inc.tax.~ x$Customer.ID,data=x,FUN=sum)
I am getting this error on running this:
Error in Summary.factor(c(1L, 1L, 1L, 1L, 1L, 1L), na.rm = FALSE) : ‘sum’ not meaningful for factors
output I am expecting is:
CustomerID YTD(2019) 2018 Average
1 415.55 355.38 138.5
2 129.94 43.31
7 493.8 164.6