将每月日期与客户和成本合并在一起

时间:2015-02-13 19:38:40

标签: r

如何将每个月度日期和销售额合并到每个客户?

custid <- c(1,1,1,2,2,5,6,8,8,8)

date <- c(20130401,  20130403,  20130504,   20130508,   20130511,   20130716,   
      20130719, 20130423,   20130729,   20130707)

cost <- c(12,  41,  89, 45.5,   32.89,  74, 76, 12, 15.78,  10)

data <- data.frame(custid, date,cost)

data$date <- as.Date(as.character(data$date), "%Y%m%d") 

aggregate(cbind(total_cost_per_month=cost)~cbind(date=format(date, '%Y-%m')), cbind(custid=custid), data, sum) 

Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L,  : 
                                  invalid 'trim' argument

我正在尝试使我的数据类似于此输出。甚至可以在这里重新格式化超过2列吗?聚合函数用于合并/堆叠日期和成本

custid  date      monthlyCost
1       201304    53
1       201305    89
2       201305    78.39
5       201307    74
6       201307    76
8       201304    27.78
8       201307    10

谢谢!

0 个答案:

没有答案