是否有比以下更有效的查询
DT[, list(length(unique(OrderNo)) ),customerID]
使用客户 ID、订单号和产品行项目细化 LONG 格式表,这意味着如果客户在该交易中购买了超过 1 个项目,则将存在具有相同订单 ID 的重复行。
尝试制定独特的购买方式。length()
按客户 ID 计算所有订单 ID,包括重复项,仅查找唯一编号。
从这里编辑:
这是一些虚拟代码。理想情况下,我正在寻找的是使用unique()
.
df <- data.frame(
customerID=as.factor(c(rep("A",3),rep("B",4))),
product=as.factor(c(rep("widget",2),rep("otherstuff",5))),
orderID=as.factor(c("xyz","xyz","abd","qwe","rty","yui","poi")),
OrderDate=as.Date(c("2013-07-01","2013-07-01","2013-07-03","2013-06-01","2013-06-02","2013-06-03","2013-07-01"))
)
DT.eg <- as.data.table(df)
#Gives unique order counts
DT.eg[, list(orderlength = length(unique(orderID)) ),customerID]
#Gives counts of all orders by customer
DT.eg[,.SD, keyby=list(orderID, customerID)][, .N, by=customerID]
^
|
This should be .N, not .SD ~ R.S.