在两列之间的最小距离上合并/合并两个数据集

时间:2019-03-04 13:38:49

标签: r dplyr data.table

我正在尝试合并两个收益率数据集,我需要在到期日的最小差异上合并它们。由于我想计算商业贷款与到期日匹配的国库券之间的利差。 联接有效,但是我正在寻找更好的方法,也许使用fuzzy_join

library(data.table)
library(zoo)
library(tidyverse)

# Commercial loan issued in 2002 Q1 with a maturity of 119 months
dt.MWE <- structure(list(Issue.Year.Quarter = structure(2002, class = "yearqtr"), Maturity.Date = structure(15385, class = "Date")
               , Issue.Months.to.Maturity = 119), row.names = c(NA,  -1L), class = c("data.table", "data.frame"))


# Treasury Yields in 2002 Q1 with different maturities
dt.Yields <- structure(list(Year.Quarter = structure(c(2002, 2002, 2002, 2002,  2002, 2002, 2002, 2002, 2002, 2002, 2002), class = "yearqtr"), 
                            Maturity = c(12, 120, 1, 24, 240, 36, 360, 3, 60, 6, 84), 
                            Avg.Treasury.Yield = c(2.32000001271566, 5.0766666730245, 1.73666663964589, 3.20333329836527, 5.74333333969116, 3.74999992052714, 
                                                   5.42499995231629, 1.75666666030884, 4.46000003814697, 1.89666664600372,  4.8799999554952))
                       , row.names = c(NA, -11L), class = c("data.table", "data.frame"))


dt.join.result <- dt.MWE %>% inner_join(x = . , y = dt.Yields
                  , by = c(Issue.Year.Quarter = "Year.Quarter")) %>% mutate(.data = ., Dist.Maturity = abs(Issue.Months.to.Maturity - Maturity))  %>% group_by(.data = .,Issue.Year.Quarter )%>% mutate(.data = ., rank.Dist.Maturity = row_number(Dist.Maturity)) %>% dplyr::filter(rank.Dist.Maturity == 1) %>% data.table(.)

>   Issue.Year.Quarter Maturity.Date Issue.Months.to.Maturity Maturity Avg.Treasury.Yield Dist.Maturity min.Dist.Maturity
1:            2002 Q1    2012-02-15                      119      120           5.076667             1                 1

1 个答案:

答案 0 :(得分:4)

使用滚动连接的解决方案

由于某种原因,data.table在处理示例数据时给了我错误,因此我创建了副本dt1dt2来使用。这些(可能)在您身边不需要...

library(data.table)

#create copies of the data.tables
dt1 <- copy( dt.MWE )
dt2 <- copy( dt.Yields )

#set keys to join on.
#the last key of each dt is using in the roll-action of the join
setkeyv( dt1, c("Issue.Year.Quarter", "Issue.Months.to.Maturity"))
setkeyv( dt2, c("Year.Quarter", "Maturity"))

#perform by reference (=fast!) rolling join to get the nearest match of the last (=second) key
dt1[, c("Maturity", "Avg.Treasury.Yield") := ( dt2[dt1, list( x.Maturity, Avg.Treasury.Yield) , roll = "nearest"])]
#calculate the absolute distance
dt1[, min.Dist.Maturity := abs( Issue.Months.to.Maturity - Maturity) ][]

#    Issue.Year.Quarter Maturity.Date Issue.Months.to.Maturity Maturity Avg.Treasury.Yield min.Dist.Maturity
# 1:               2002    2012-02-15                      119      120           5.076667                 1