所有订单的最后订单总和和平均成本

时间:2014-11-12 18:26:37

标签: r

数据:

DB <- data.frame(orderID  = c(1,2,3,4,5,6,7,8,9,10),     
orderDate = c("1.1.14","1.1.14","1.1.14","1.1.14","1.1.14", "1.1.14","1.1.14","2.1.14","2.1.14","2.1.14"),  
itemID = c(2,3,2,5,12,4,2,3,1,5),  
price = c(29.90, 39.90, 29.90, 19.90, 49.90, 9.90, 29.90, 39.90, 14.90, 19.90),
customerID = c(1, 2, 3, 1, 1, 3, 2, 2, 1, 1))

预期结果:

orderValueOfLastOrder = c(34.80, 39.90, 39.80, 34.80, 34.80, 39.80, 39.90, 39.90, 34.80, 34.80) 
AverageValue = c(67.25, 54.85, 39.80, 67.25, 67.25, 39.80, 54.85, 54.85, 67.25, 67.25)

您好, 我为老板解决了一个问题,所以如果你窥视帮助我,我会很高兴:) 在数据集中,每个订单都有自己的ID,每个注册用户都有自己唯一的customerID。每个客户都可以订购具有特定价格的物品(带有ItemID)。 我想总结订单的价格,但我有两个问题: 1.我想要每个客户的最后订单的价值(总和)((从每个用户的最后订单中总结所有订购商品的价格[今天的日期例如是15.10.14]) 2.每个订单的每个订单的平均价格[对于他的订单] 我还想将结果添加为现有数据集中的新列...

我已经尝试过了:

DB1$orderValueOfLastOrder<- with(DB1, ave(as.numeric(DB1$orderDate), 
price, FUN = function(x) sum(x ==max(x)))) 

但它不起作用......

所以,请你用“ave”函数向我展示一种方法[我更喜欢用ave做这件事(如果不可能的话,我会非常满意其他所有解决方案:))

非常感谢您的支持!

1 个答案:

答案 0 :(得分:2)

这是使用data.table

的简单而有效的解决方案
library(data.table)
setDT(DB)[, orderDate := as.Date(orderDate, format = "%d.%m.%y")]
DB[, `:=` (orderValueOfLastOrder  = sum(price[orderDate == max(orderDate)]),
           AverageValue = sum(price)/length(unique(orderDate))), by = customerID]

如果您希望保留data.frame格式,请使用setDF(DB)(您需要data.table v 1.9.4&gt; =)或DB <- as.data.frame(DB)(对于旧版本)

setDF(DB)[]
#    orderID  orderDate itemID price customerID orderValueOfLastOrder AverageValue
# 1        1 2014-01-01      2  29.9          1                  34.8        67.25
# 2        2 2014-01-01      3  39.9          2                  39.9        54.85
# 3        3 2014-01-01      2  29.9          3                  39.8        39.80
# 4        4 2014-01-01      5  19.9          1                  34.8        67.25
# 5        5 2014-01-01     12  49.9          1                  34.8        67.25
# 6        6 2014-01-01      4   9.9          3                  39.8        39.80
# 7        7 2014-01-01      2  29.9          2                  39.9        54.85
# 8        8 2014-01-02      3  39.9          2                  39.9        54.85
# 9        9 2014-01-02      1  14.9          1                  34.8        67.25
# 10      10 2014-01-02      5  19.9          1                  34.8        67.25
class(DB)
## [1] "data.frame"