我有兩個表,我想通過時間戳連接,但是,時間戳並不是在完全相同的時間記錄的。 因此,我希望加入任何彼此相差 5 分鐘以內的時間戳。 如果時間戳在其他表中沒有匹配的時間戳,我仍然希望保留行數據。 在 JSL 中是否有一種簡單的方法可以做到這一點?
您可以在每個數據表中創建一個公式列,將時間戳列四捨五入到最接近的 5 分鐘,然後使用這些列進行聯接。在JMP 14 中,如果右鍵單擊時間戳列的標題,然後選擇新建公式列-> 變換-> 舍入...,您將看到一個對話框,提供向上舍入、向下舍入或舍入的選項到您想要的最接近的時間增量。如果您沒有 JMP 14,舍入到最近的 5 分鐘間隔的公式將如下所示:
回合(:時間戳/300)*300
(注意,300 是 5 分鐘的秒數)。
表 -> JMP 查詢構建器(或表 -> 聯接)對話框具有用於保留不匹配項和創建匹配標誌列的選項。
希望這可以幫助。
沒有一個內置平台可以像您所說的那樣進行模糊匹配的連接。不過,JSL 可以用來滿足您的需要。
以下是編寫請求腳本的一種方法的粗略示例
Names Default To Here( 1 );
// Create sample data tables
dt1 = New Table( "Data Table 1",
Add Rows( 100 ),
New Column( "DateTime",
Numeric,
"Continuous",
Format( "m/d/y h:m", 19 ),
Input Format( "m/d/y h:m" ),
Formula( Today() + Row() * In Minutes( 5 ) ),
Set Selected
)
);
dt1 << run formulas;
dt1:DateTime << delete formula;
dt2 = dt1 << Subset(
Sampling Rate( 0.25 ),
Selected columns only( 0 ),
output table name( "Data Table 2" ),
link to original table( 0 )
);
Current Data Table( dt2 );
For Each Row( :DateTime = :DateTime + In Minutes( Random Uniform( -100, 100 ) ) );
dt2 << sort( by( :DateTime ), order( ascending ), replace table( 1 ) );
// Create a column in data table 1 for reference
dt1 << New Column( "RowNum", formula( Row() ) );
dt1:RowNum << delete formula;
// Create in data table 2 a column that will hold the row to join to
dt2 << New Column( "Match Row" );
// Loop through data table 2 and find the rows in the first table that match
For( i = 1, i <= N Rows( dt2 ), i++,
checkVal = dt2:DateTime[i];
theRows = dt1 << get rows where(
:Datetime > checkVal - In Minutes( 5 ) & :DateTime < checkVal + In Minutes( 5 )
);
If( N Rows( theRows ) > 0,
minList = {};
For( k = 1, k <= N Rows( theRows ), k++,
Insert Into( minList, Abs( dt1:DateTime[theRows[k]] - checkVal ) )
);
dt2:Match Row[i] = theRows[Loc( minList, Min( minList ) )];
,
dt2:Match Row[i] = 0
);
);
// Join the tables
dt3 = dt1 << Join(
With( dt2 ),
Merge Same Name Columns,
By Matching Columns( :RowNum = :Match Row ),
Drop multiples( 0, 0 ),
Include Nonmatches( 1, 1 ),
Preserve main table order( 1 ),
output table name("Final Table")
);