我有一些数据要用来计算最近一个订单日期以来最近3个月的平均销售额,还想计算该客户的年初至今销售额以及所有groupby customerID。在Orderstatus中,我只想计算已发货和部分已发货的订单
我的数据如下:
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
我曾尝试通过customerid对其进行汇总,但是在orderstatus中,我只希望状态被发送的记录。我无法启用该过滤器,也不知道平均只需要3个月。
我尝试了以下代码:
x <- aggregate(x$Order.Total..inc.tax.~ x$Customer.ID,data=x,FUN=sum)
我在运行此错误:
Summary.factor(c(1L,1L,1L,1L,1L,1L),na.rm = FALSE)错误: “总和”对因素没有意义
我期望的输出是:
CustomerID YTD(2019) 2018 Average
1 415.55 355.38 138.5
2 129.94 43.31
7 493.8 164.6
答案 0 :(得分:-1)
尝试将数据从因子转换为数字/整数。