-1
我正在计算每个客户ID的平均值或平均值。对于下面的数据:R中每个客户的平均成本
customerID <- c(1,1,1,1,2,2,2,2,3,3)
dates <- c(20130401, 20130403, 20130504, 20130508, 20130511,
20130716, 20130719, 20130723, 20130729, 20130907)
cost <- c(12, 41, 89, 45.5, 32.89, 74, 76, 12, 15.78, 10)
data <- data.frame(customerID, dates,cost)
data$dates <- as.Date(as.character(data$dates), "%Y%m%d")
# data2 <- aggregate(cbind(average_cost=cost) + customerID, data, mean)
的数据是这样的:
customerID dates cost
1 20130401 12
1 20130403 41
1 20130504 89
1 20130508 45.5
2 20130511 32.89
2 20130716 74
2 20130719 76
2 20130723 12
3 20130729 15.78
3 20130907 10
我怎样才能得到类似这样的输出?我可以得到整个数据集的平均值,但不是每个客户的ID。谢谢!
customerID average_cost
1 46.875
2 48.7225
3 12.89
@大卫arenburg。是的,它是不同的。以前,它是为每个客户查找总数以及日期。这个问题只涉及每个客户的客户和平均水平。日期在这里并不重要,就像他们对前面的问题所做的一样。数据集值似乎相似,但我尝试了不同的场景。 – sharp 2015-02-23 14:59:56
这个问题是dupe的一个特例。 – 2015-02-23 15:00:59
@DavidArenburg。原来如此。没有看到顶部的链接。谢谢。 – sharp 2015-02-23 15:04:17