[英]How do you change the data table rolling join condition from weak inequality to strict inequality?
[英]Rolling sum with inequality conditionals
我正在處理來自多家公司的一些歷史財務數據。
我的 data.frames/tables 具有以下形式:
library(data.table)
dt <- data.table(company = rep(1:2, each = 9),
period = c(1, 2, 2, 2, 3, 3, 4, 5, 6), # recycled
result = c(1, 2, 3, 4, 5, 6, 7, 8, 9), # recycled
filingDate = as.Date(c('2000-01-01', # 1 1
'2000-02-01', # 2 2
'2000-02-02', # 2 3
'2000-03-02', # 2 4
'2000-03-01', # 3 5
'2000-03-02', # 3 6
'2000-04-01', # 4 7
'2000-05-01', # 5 8
'2000-06-01', # 6 9
# company 2
'2000-03-01', # 1 1
'2000-04-01', # 2 2
'2000-04-02', # 2 3
'2000-05-02', # 2 4
'2000-05-01', # 3 5
'2000-06-02', # 3 6
'2000-06-01', # 4 7
'2000-07-01', # 5 8
'2000-08-01'))) # 6 9
dt
#> company period result filingDate
#> 1: 1 1 1 2000-01-01
#> 2: 1 2 2 2000-02-01
#> 3: 1 2 3 2000-02-02
#> 4: 1 2 4 2000-03-02
#> 5: 1 3 5 2000-03-01
#> 6: 1 3 6 2000-03-02
#> 7: 1 4 7 2000-04-01
#> 8: 1 5 8 2000-05-01
#> 9: 1 6 9 2000-06-01
#> 10: 2 1 1 2000-03-01
#> 11: 2 2 2 2000-04-01
#> 12: 2 2 3 2000-04-02
#> 13: 2 2 4 2000-05-02
#> 14: 2 3 5 2000-05-01
#> 15: 2 3 6 2000-06-02
#> 16: 2 4 7 2000-06-01
#> 17: 2 5 8 2000-07-01
#> 18: 2 6 9 2000-08-01
由reprex 包(v0.3.0) 於 2019 年 12 月 26 日創建
所以我在這里有兩家不同公司在 6 個時期(即季度)報告的結果。 然而,公司有時會注意到他們報告了錯誤,在這種情況下他們會重述。 在此示例中,公司必須對期間 2 進行兩次重述(總共三份報告),對期間 3 進行重述。
提交這些報告/重述的日期顯示在filingDate
。
現在,我想計算每家公司隨時間推移的兩個(或更多)期間的已知滾動總和。
如果我們每個時期只有一份申請,那么可以通過dt[, rSum = frollsum(result, 2), by = company]
實現。
但是,由於我們每個期間可以有多個申請,我們需要將這些過濾到“當前日期”已知的最新申請,因為我們可以在以下期間已經提交之后重新聲明期間。 一個例子是公司 1 在 2000 年 3 月 2 日的期間 2 重述,在期間 3 於 2000 年 3 月 1 日提交之后。
因此,預期的輸出將是:
#> company period rSum filingDate
#> 1: 1 1 1 2000-01-01
#> 2: 1 2 3 2000-02-01 # 2 + 1
#> 3: 1 2 4 2000-02-02 # 3 + 1
#> 4: 1 2 5 2000-03-02 # 4 + 1 (optional row, see below)
#> 5: 1 3 8 2000-03-01 # 5 + 3
#> 6: 1 3 10 2000-03-02 # 6 + 4
#> 7: 1 4 13 2000-04-01 # 7 + 6
#> 8: 1 5 15 2000-05-01 # 8 + 7
#> 9: 1 6 17 2000-06-01 # 9 + 8
#> 10: 2 1 1 2000-03-01
#> 11: 2 2 3 2000-04-01 # 2 + 1
#> 12: 2 2 4 2000-04-02 # 3 + 1
#> 13: 2 3 8 2000-05-01 # 5 + 3
#> 14: 2 3 9 2000-05-02 # 5 + 4
#> 15: 2 4 12 2000-06-01 # 7 + 5
#> 16: 2 4 13 2000-06-02 # 7 + 6
#> 17: 2 5 15 2000-07-01 # 8 + 7
#> 18: 2 6 17 2000-08-01 # 9 + 8
這里有一些細微差別應該明確。
查看輸入表dt
的第 4 行和第 6 行,我們看到公司 1 在 2000 年 3 月 2 日對期間 2 和 3 進行了兩次重述。由於我們有期間 3 的結果,因此我們不再處於期間 2,因此相關的“最近的” rSum
是第 3 期的。 這就是為什么上面的預期輸出將第 4 行表示為可選的原因:我真的只關心第 6 行的rSum
,但任何包含第 4 行虛擬結果的解決方案也非常好。
更復雜的是輸入的第 15 行和預期輸出的第 16 行發生的情況:公司 2 於 2000 年 6 月 2 日重述了其第 3 期的結果。 但是,到那時我們已經有了第 4 階段的結果(於 2000-06-01 給出)。 因此,再一次,該日期的“最近” rSum
實際上在第 3 和第 4 期( 6 + 7 = 13
)之間,並且應該與最近的第 4 期而非第 3 期相關聯。
我看不出如何優雅地做到這一點。 我能想到的就是獲取所有唯一的提交日期並通過它們進行 for 循環,以便獲得以前時期的最新結果。 請注意,這甚至不能滿足上面的第二個細微差別,因為 2000-06-02 的結果與時期 3 相關聯。
r <- c()
for(comp in unique(dt$company)) {
x <- dt[company == comp]
for(d in x[, sort(unique(filingDate))]) {
y <- tail(x[filingDate <= d,
.SD[filingDate == max(filingDate)],
by = .(company, period)], 2)
r <- c(r, sum(y$result))
}
}
x <- dt[, .SD[period == max(period)],
by = .(company, filingDate)]
setorder(x, company, filingDate, period)
x[, rSum := r]
setorder(x, company, period, filingDate)
x
#> company filingDate period result rSum
#> 1: 1 2000-01-01 1 1 1
#> 2: 1 2000-02-01 2 2 3
#> 3: 1 2000-02-02 2 3 4
#> 4: 1 2000-03-01 3 5 8
#> 5: 1 2000-03-02 3 6 10
#> 6: 1 2000-04-01 4 7 13
#> 7: 1 2000-05-01 5 8 15
#> 8: 1 2000-06-01 6 9 17
#> 9: 2 2000-03-01 1 1 1
#> 10: 2 2000-04-01 2 2 3
#> 11: 2 2000-04-02 2 3 4
#> 12: 2 2000-05-02 2 4 9
#> 13: 2 2000-05-01 3 5 8
#> 14: 2 2000-06-02 3 6 13 # should be Period 4
#> 15: 2 2000-06-01 4 7 12
#> 16: 2 2000-07-01 5 8 15
#> 17: 2 2000-08-01 6 9 17
但是,這感覺非常不合理,那么有沒有更慣用的方法來做到這一點?
一種選擇是使用非對等連接:
winsz <- 2L
dt[, c("start", "end") := .(period - winsz + 1L, period)]
dt[, rSum :=
dt[dt, on=.(company, end>=start, end<=end),
by=.EACHI, i.result +
#filter right table for rows with periods before current period and filingDate before current filingDate
#and select the latest result for each period and then sum those result
sum(.SD[period<i.period & filingDate<=i.filingDate, result[.N], period]$V1)
]$V1
]
輸出:
company period result filingDate start end rSum
1: 1 1 1 2000-01-01 0 1 1
2: 1 2 2 2000-02-01 1 2 3
3: 1 2 3 2000-02-02 1 2 4
4: 1 2 4 2000-03-02 1 2 5
5: 1 3 5 2000-03-01 2 3 8
6: 1 3 6 2000-03-02 2 3 10
7: 1 4 7 2000-04-01 3 4 13
8: 1 5 8 2000-05-01 4 5 15
9: 1 6 9 2000-06-01 5 6 17
10: 2 1 1 2000-03-01 0 1 1
11: 2 2 2 2000-04-01 1 2 3
12: 2 2 3 2000-04-02 1 2 4
13: 2 2 4 2000-05-02 1 2 5
14: 2 3 5 2000-05-01 2 3 8
15: 2 3 6 2000-06-02 2 3 10
16: 2 4 7 2000-06-01 3 4 12
17: 2 5 8 2000-07-01 4 5 15
18: 2 6 9 2000-08-01 5 6 17
數據:
library(data.table)
dt <- data.table(company = rep(1:2, each = 9),
period = c(1, 2, 2, 2, 3, 3, 4, 5, 6),
result = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
filingDate = as.Date(c('2000-01-01','2000-02-01','2000-02-02','2000-03-02','2000-03-01','2000-03-02','2000-04-01','2000-05-01','2000-06-01','2000-03-01','2000-04-01','2000-04-02','2000-05-02','2000-05-01','2000-06-02','2000-06-01','2000-07-01','2000-08-01')))
dt[, filingDate := as.IDate(filingDate, format="%Y-%m-%d")]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.