我有两张表,我需要做一个总结.表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
但不能完全弄清楚.
好问题.你基本上要做的是加入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
到位x1
(由@Frank建议):
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