我有两个表需要做一个sumif。表1包含时间段,即年末年和季度(即4
,8
,12
等)。表2包含了3
,6
,7
等季度的年度交易。
我需要表3来汇总一年中的所有交易,以便在年底获得累积头寸。
以下是一些示例代码,用于解释数据的外观以及输出的外观:
library(data.table)
x1 <- data.table("Name" = "LOB1", "Year" = 2000,
"Quarter" = c(4, 8, 12, 16, 20, 24, 28, 32, 36))
x2 <- data.table("Name" = "LOB1", "Year" = 2000,
"Quarter" = c(3, 6, 7, 9, 11, 14, 16, 20, 24),
"Amount" = c(10000, 15000, -2500, 3500, -6500, 25000,
11000, 9000, 7500))
x3 <- data.table("Name" = "LOB1", "Year" = 2000,
"Quarter" = c(4, 8, 12, 16, 20, 24, 28, 32, 36),
"Amount" = c(10000, 22500, 19500, 55500, 64500, 72000,
72000, 72000, 72000))
我已尝试过merge
,summarise
,foverlaps
,但无法弄明白。
答案 0 :(得分:11)
好问题。您基本上要做的是按Name
,Year
和Quarter <= Quarter
加入,同时汇总所有匹配的Amount
值。这可能是使用新的非equi连接(在最新的稳定版本的data.table v-1.10.0中引入)和foverlaps
(后者可能是次优的)
非Equi加入:
x2[x1, # for each value in `x1` find all the matching values in `x2`
.(Amount = sum(Amount)), # Sum all the matching values in `Amount`
on = .(Name, Year, Quarter <= Quarter), # join conditions
by = .EACHI] # Do the summing per each match in `i`
# Name Year Quarter Amount
# 1: LOB1 2000 4 10000
# 2: LOB1 2000 8 22500
# 3: LOB1 2000 12 19500
# 4: LOB1 2000 16 55500
# 5: LOB1 2000 20 64500
# 6: LOB1 2000 24 72000
# 7: LOB1 2000 28 72000
# 8: LOB1 2000 32 72000
# 9: LOB1 2000 36 72000
作为旁注,您可以在Amount
(由@Frank提议)中轻松添加x1
:
x1[, Amount :=
x2[x1, sum(x.Amount), on = .(Name, Year, Quarter <= Quarter), by = .EACHI]$V1
]
如果您在该表中只有三个连接列,那么这可能会很方便。
<强> foverlaps:强>
你提到了foverlaps
,所以理论上你也可以使用这个函数来实现同样的功能。虽然我担心你很容易忘记。使用foverlaps
,您需要创建一个巨大的表格,其中x2
中的每个值都会多次连接到x1
中的每个值,并将所有内容存储在内存中
x1[, Start := 0] # Make sure that we always join starting from Q0
x2[, Start := Quarter] # In x2 we want to join all possible rows each time
setkey(x2, Name, Year, Start, Quarter) # set keys
## Make a huge cartesian join by overlaps and then aggregate
foverlaps(x1, x2)[, .(Amount = sum(Amount)), by = .(Name, Year, Quarter = i.Quarter)]
# Name Year Quarter Amount
# 1: LOB1 2000 4 10000
# 2: LOB1 2000 8 22500
# 3: LOB1 2000 12 19500
# 4: LOB1 2000 16 55500
# 5: LOB1 2000 20 64500
# 6: LOB1 2000 24 72000
# 7: LOB1 2000 28 72000
# 8: LOB1 2000 32 72000
# 9: LOB1 2000 36 72000