我想知道是否可以使用data.table的非等自我加入来计算新发票发行日期之前未结发票的数量。
以下是演示数据:
library(data.table)
demo <- data.table(
ClientId = c(1, 2, 2, 2, 3, 4, 4, 4, 4, 5, 6, 7),
IssueDate = as.Date(c("2016-12-09", "2015-10-23", "2015-12-01", "2016-09-08", "2016-10-10", "2014-10-02", "2015-10-26", "2016-10-03", "2016-12-27",
"2017-03-29", "2017-03-29", "2017-05-22")),
ClearingDate = as.Date(c("2017-07-06", "2015-10-26", "2015-12-02", "2016-09-09", "2016-11-15", "2014-11-18", "2015-12-14", "2017-01-13", "2017-01-13",
"2017-06-05", "2017-04-12", "2017-06-19")),
Amount = c(517, 45, 79, 1891, 603, 15, 1706, 975, 53, 78, 92, 158))
这里是计算总付款发票数量和总付款发票金额总和的一个例子。
demo[demo, .(num_total_paid_invoices = .N,
sum_total_paid_invoices = sum(Amount)),
on = .(ClientId,
ClearingDate < IssueDate),
by = .EACHI]
ClientId ClearingDate num_total_paid_invoices sum_total_paid_invoices
1: 1 2016-12-09 0 NA
2: 2 2015-10-23 0 NA
3: 2 2015-12-01 1 45
4: 2 2016-09-08 2 124
5: 3 2016-10-10 0 NA
6: 4 2014-10-02 0 NA
7: 4 2015-10-26 1 15
8: 4 2016-10-03 2 1721
9: 4 2016-12-27 2 1721
10: 5 2017-03-29 0 NA
11: 6 2017-03-29 0 NA
12: 7 2017-05-22 0 NA
我认为这会奏效,但没有。
demo[demo, .(
num_total_outstanding_invoices = .N,
sum_total_outstanding_invoices = sum(Amount)
),
on = .(ClientId,
IssueDate < IssueDate,
ClearingDate > IssueDate
),
by = .EACHI]
提前致谢!