あなたが尋ねていると私が信じていることを述べさせてください。
プラスXマイナス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時間以上離れている隣人の近くの赤い旗
最も近い行の呼び出しの[1] [1]は、返されたリストから最初の項目(1つの要素の行列)を取得し、その要素をスカラー値として取得しています。行番号です。
非対称ウィンドウが必要な場合、この手法は機能しません(B時間は2時間以内である必要があるなど) 後時間)。2時間以内にすべての行を取得し、動作する最初の行を選択することで、これを調整できます。