gpt4 book ai didi

r - 带双面卷的全外连接 (LOCF)

转载 作者:行者123 更新时间:2023-12-02 03:32:14 26 4
gpt4 key购买 nike

如何通过完全外连接有效地合并两个 data.table,同时通过在左侧和右侧滚动最后一个观察值 (LOCF) 来处理缺失值?

现实世界应用 - 有两个不一定交错的交易规则信号表,XY、持有(稀疏) 随时间变化的信号值。总体目标是定义复合信号,其中Signal.z = Signal.x AND Signal.y

X <- data.table(Instrument=rep("SPX",3)
, Date=as.IDate(c("2013-11-20","2013-11-22","2013-11-24"))
, Signal=c(TRUE,FALSE,TRUE), key=c("Instrument", "Date"))

Y <- data.table(Instrument=rep("SPX",3)
, Date=as.IDate(c("2013-11-21","2013-11-23","2013-11-25"))
, Signal=c(FALSE,TRUE,FALSE), key=c("Instrument", "Date"))

期望的结果:

   Instrument       Date Signal.x Signal.y Signal.z
1: SPX 2013-11-20 TRUE NA NA
2: SPX 2013-11-21 TRUE FALSE FALSE
3: SPX 2013-11-22 FALSE FALSE FALSE
4: SPX 2013-11-23 FALSE TRUE FALSE
5: SPX 2013-11-24 TRUE TRUE TRUE
6: SPX 2013-11-25 TRUE FALSE FALSE

最佳答案

也许是这样的:

dates = sort(c(X$Date, Y$Date))

setkey(X, Date)
setkey(Y, Date)

Z = X[J(dates), roll = T][,
Signal.y := Y[J(dates), roll = T]$Signal][,
Signal.z := as.logical(Signal * Signal.y)]

基于这个想法,以下是针对大型示例数据执行此操作的方法:

# assuming keys are set to Instrument, Date in both data.tables

Z = unique(setkey(rbind(setnames(X[Y, roll = T],
c("Instrument", "Date", "Signal.x", "Signal.y")),
setnames(Y[X, roll = T],
c("Instrument", "Date", "Signal.y", "Signal.x")),
use.names = TRUE),
Instrument, Date))[,
Signal.z := as.logical(Signal.x * Signal.y)]

关于r - 带双面卷的全外连接 (LOCF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20199374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com