計算滯後變化的其他列層級的列差異我有一列:EVENT(字元標稱),具有各種值。還有另一列:日期時間(數字連續)。我有 :datetime 列按升序排序。我感興趣的是讓另一列計算每當有“CONNECT”事件時,它與最後一個“DISCONNECT”事件之間的時間差是多少。問題是連接和之前的斷開之間的延遲可能會有所不同(見下圖),我也更喜歡保留這種高表格格式。日期時間升序排序的解決方案值得讚賞,但不排序也將是有益的。

基礎資料分析與建模數據探索和視覺化回覆:計算滯後變更的其他列層級的列差異例如,您可以在公式中使用變數來儲存上次斷開連接的值
Names Default To Here(1);
dt = New Table("Untitled",
Add Rows(12),
Compress File When Saved(1),
New Column("E", Character, "Nominal", Set Values({"A", "B", "Disc", "Con", "A", "B", "C", "B", "Disc", "A", "B", "Con"})),
New Column("T", Numeric, "Continuous", Format("Best", 12), Set Values([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]))
);
dt << New Column("Dif", Numeric, Continuous, Formula(
If(Row() == 1,
last_disc = .;
retval = .;
);
If(:E == "Disc",
last_disc = :T;
retval = .;
, :E == "Con",
retval = :T - last_disc
,
retval = .;
);
retval;
));

回覆:計算滯後變更的其他列層級的列差異這個方法效果很好,謝謝!
回覆:計算滯後變更的其他列層級的列差異您也可以使用矩陣
Names Default To Here(1);
dt = New Table("Untitled",
Add Rows(12),
Compress File When Saved(1),
New Column("E", Character, "Nominal", Set Values({"A", "B", "Disc", "Con", "A", "B", "C", "B", "Disc", "A", "B", "Con"})),
New Column("T", Numeric, "Continuous", Format("Best", 12), Set Values([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]))
);
events = :E << get values;
times = :T << get values;
discs = Loc(events, "Disc");
cons = Loc(events, "Con");
difs = J(1, N Rows(dt), .);
difs[cons] = times[cons] - times[discs];
dt << new column("difs", numeric, continuous, values(difs));
這確實有很多假設,但對於各種邊緣情況可以變得更加穩健(Loc Sorted 對於類似的事情很有幫助,有時甚至是 KDTable/VPTree)。
回覆:計算滯後變更的其他列層級的列差異稍微麻煩一點的是,有時在斷開事件出現之前,連線事件可能會出現幾次。在這種情況下,我希望僅在 DISCONNECT 事件之後出現的第一個CONNECT 事件上計算差異。是否對腳本進行了輕微修改以適應這種情況?再次感謝您的幫忙。
回覆:計算滯後變更的其他列層級的列差異這是@jthi程式碼的修改,應該可以處理這個問題。
Names Default To Here(1);
dt = New Table("Untitled",
Add Rows(12),
Compress File When Saved(1),
New Column("E", Character, "Nominal", Set Values({"A", "B", "Disc", "Con", "A", "B", "C", "B", "Disc", "A", "B", "Con"})),
New Column("T", Numeric, "Continuous", Format("Best", 12), Set Values([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]))
);
dt << New Column("Dif", Numeric, Continuous, Formula(
If(Row() == 1,
last_disc = .;
retval = .;
);
If(:E == "Disc",
last_disc = :T;
retval = .;
firstCon = .;
, :E == "Con",
If(ismissing(firstcon), firstcon=:t);
retval = firstcon - last_disc
,
retval = .;
);
retval;
));
我建議您花時間閱讀 JMP 幫助下的腳本指南。另外,請花時間研究響應論壇成員提供的程式碼。對 Jarmo 腳本的修改不需要任何新的腳本知識。它只需要在腳本流程中添加一些額外的邏輯。使用者的目標應該是能夠對腳本進行自己的增強