让我说一下,我相信你在问什么。
您想知道如何在正负X小时数范围内匹配2个日期列。
它是否正确?
是在同一数据表中的列,还是要在数据表之间进行匹配,以便可以将数据合并在一起?
如果在+/-小时范围内找到多于一行,是否要选择最接近的值?如果打成平局怎么办?
dt = New Table( "Untitled 3",
Add Rows( 20 ),
New Column( "atime",
Numeric,
"Continuous",
Format( "ddMonyyyy h:m", 19 ),
Input Format( "ddMonyyyy h:m" ),
Formula( As Date( Random Uniform( Today() - 100000, Today() + 100000 ) ) ),
Set Display Width( 250 )
),
New Column( "closest b time",
Numeric,
"Continuous",
Format( "ddMonyyyy h:m", 19 ),
Input Format( "ddMonyyyy h:m" ),
Set Display Width( 250 )
),
New Column( "btime",
Numeric,
"Continuous",
Format( "ddMonyyyy h:m", 19 ),
Input Format( "ddMonyyyy h:m" ),
Formula( As Date( Random Uniform( Today() - 100000, Today() + 100000 ) ) ),
Set Display Width( 250 )
),
New Column( "closest a time",
Numeric,
"Continuous",
Format( "ddMonyyyy h:m", 19 ),
Input Format( "ddMonyyyy h:m" ),
Set Display Width( 250 )
)
);
dt << runformulas;
atime << deleteformula;
btime << deleteformula;
// here it is: find both backwards and forwards nearest neighbor
kdtA = KDTable( dt:atime << getvalues );
kdtB = KDTable( dt:btime << getvalues );
For( irow = 1, irow <= N Rows( dt ), irow += 1,
// look up the row of the nearest A time to this row's B time
closeA = (kdtA << knearestrows( 1, Matrix( dt:btime[irow] ) ))[1][1];
// get the actual A time from that row and keep it in this row.
dt:closest a time[irow] = dt:atime[closeA];
// flag times that are too far away
if(abs(dt:closest a time[irow]-btime[irow])>inhours(2),dt:closest a time<<COLOR cells="">inhours(2),dt:closest b time<<COLOR cells=""></COLOR></COLOR>
邻居之间的红旗间隔时间超过2小时
knearest行调用中的[1] [1]从返回的列表中获取第一项,该列表是一个元素的矩阵,然后将该元素作为标量值。它是行号。
如果您需要非对称窗口(例如B时间必须在2小时之内),则此技术将不起作用后一次)。您可以通过在2小时内获取所有行,然后选择第一个行来进行调整。