我有一个data.table键入(cust,loc,product) 每个组合键都有多个条目。 对于这个表的模拟,可以生成如下(原始表格要大得多,但这似乎重现了这个问题):
set.seed(1)
IM_Demand_DT <- data.table(cust="11", loc="11", product="P1", tsum=runif(10,0,100), priority=runif(10,0,1))
setDT(IM_Demand_DT, key=c("cust", "loc", "product"))
我可以返回选定的行:
IM_Demand_DT[list(loc="11", product="P1"),
.(cust, loc, product, tsum, priority, .I), on=c("loc","product")]
# cust loc product tsum priority I
# 1: 11 11 P1 12.97134 0.92240856 1
# 2: 11 11 P1 98.22407 0.53635379 2
# 3: 11 11 P1 82.67184 0.04616465 3
# 4: 11 11 P1 24.23550 0.37018108 4
# 5: 11 11 P1 85.68853 0.66773850 5
# 6: 11 11 P1 84.08788 0.61719564 6
# 7: 11 11 P1 34.21633 0.49358856 7
# 8: 11 11 P1 70.62672 0.79673830 8
# 9: 11 11 P1 62.12432 0.04203652 9
#10: 11 11 P1 65.37663 0.64593408 10
我想返回按优先级列
排序的这些行IM_Demand_DT[list(loc="11", product="P1"),
.(cust, loc, product, tsum, priority, .I), on=c("loc","product"), keyby=priority]
# priority cust loc product tsum priority I
# 1: 0.04203652 11 11 P1 70.62672 0.04203652 8
# 2: 0.04616465 11 11 P1 24.23550 0.04616465 4
# 3: 0.37018108 11 11 P1 34.21633 0.37018108 7
# 4: 0.49358856 11 11 P1 65.37663 0.49358856 10
# 5: 0.53635379 11 11 P1 82.67184 0.53635379 3
# 6: 0.61719564 11 11 P1 84.08788 0.61719564 6
# 7: 0.64593408 11 11 P1 12.97134 0.64593408 1
# 8: 0.66773850 11 11 P1 98.22407 0.66773850 2
# 9: 0.79673830 11 11 P1 85.68853 0.79673830 5
#10: 0.92240856 11 11 P1 62.12432 0.92240856 9
优先级列已正确排序,但其余列按随机顺序排列。
例如,优先级为0.04203652的第一行应该具有tsum = 62.12432且I = 9,但在此“已排序”输出中,它具有tsum = 70.62672且I = 8.
这是一个错误,还是我在这里打破了语法规则?