根據條件進行行間計算嘿,我是 JMP 腳本的新手,但對 python 語言有點熟悉。我正在嘗試使用下面的虛擬測試表實現以下目標:
1) 循環遍歷所有行(包括行前和行後),僅對 Cuvette == 11 的行進行計算,其餘 Cuvette != 11 的行「輸出」可以設定為 NA 或其他值。
2)計算本身位於兩行獨立的行之間。在 Cuvette == 11 的行中,該行的 850 nm 單元格必須與滿足兩個條件的行中的 250 nm 單元格分開:
a) 比色皿 == 12
b) 各行之間的通過值必須匹配,例如,比色皿 11 的通過值 == 比色皿 12 的通過值
| 經過 |
850 奈米 |
250 奈米 |
比色皿 |
輸出 |
| 1 |
100 |
200 |
11 |
100/500 = 0.2 |
| 2 |
400 |
500 |
11 |
400 / 400 =1 |
| 3 |
300 |
100 |
11 |
300/900 = 0.33 |
| 1 |
700 |
500 |
12 |
那 |
| 2 |
200 |
400 |
12 |
那 |
| 3 |
400 |
900 |
12 |
那 |
| 1 |
400 |
800 |
十三 |
那 |
| 2 |
500 |
700 |
十三 |
那 |
| 3 |
300 |
700 |
十三 |
那 |
自動化和腳本基礎資料分析與建模回覆:根據條件進行行間計算若要以類似 JMP 的方式執行此操作,您通常會分割數據,然後執行計算( JMP 不是電子表格),然後將其重新合併或繼續使用分割資料。
但是您也可以使用 JSL 或公式來進行此計算,但首先要考慮拆分選項。
回覆:根據條件進行行間計算歡迎來到社區。
以下是我如何處理你的問題的一個例子

Names Default To Here( 1 );
dt = Current Data Table();
// Separate out the Cuvette==12 rows into a separate data table
// and then join the Pass==Pass rows to the Cuvette==11 rows
// Then calculate the Output column
// Select the Cuvette==12 rows
dt << Select Where( :Cuvette == 12 );
// Create a new subsetted data table with those rows
dt12 = dt << Subset( Selected Rows( 1 ), columns( :Pass, :"250 nm"n ) );
// Change 250 nm column name so when joined back to original table
// it will have a unique column name
dt12:"250 nm"n << Set Name( "Divisor" );
// For alignment reasons, add a new column called Cuvette and set
// it equal to 11
dt12 << New Column( "Cuvette", Set Each Value( 11 ) );
// Join the tables using the Update Platform
dt << Update(
With( dt12 ),
Match Columns( :Pass = :Pass, :Cuvette = :Cuvette )
);
// Create the new Output column
dt << New Column( "Output", Set Each Value( :"850 nm"n / :Divisor ) );
// Remove Divisor column since it is no longer needed
dt << delete columns( Divisor );
// Close the subsetted data table since it is no longer needed
Close( dt12, nosave );
// Remove the Selected RowStates from the data table
dt << clear rowstates;
以下是一種更直接的方法,但效率更低,但卻能產生相同的結果。在大型資料表上,此方法會非常慢
Names Default To Here( 1 );
dt = Current Data Table();
dt << New Column( "Output",
Formula(
foundRow = [];
If( :Cuvette == 11,
curPass = :Pass;
foundRow = Current Data Table() <<
get rows where( :Cuvette == 12 & :Pass == curPass );
If( N Items( foundRow ) > 0,
:"850 nm"n / :"250 nm"n[foundRow[1]]
);
);
)
);
回覆:根據條件進行行間計算這是一個矩陣選項,但使用 JMP 功能通常更容易理解
Names Default To Here(1);
dt = Current Data Table();
cuvette_start = 11;
cuvette_end = 12;
cuvettes = dt[0, "Cuvette"];
// Data is sorted -> we can just take values and make comparison
start_idx = Loc(cuvettes, cuvette_start);
end_idx = Loc(cuvettes, cuvette_end);
// differences
output = J(1, N Rows(dt), .);
output[start_idx] = dt[start_idx, "850 nm"] :\</img> dt[end_idx, "250 nm"];
dt << new column("R", Numeric, Continuous, Values(output));
如果你非常依賴訂單,你可以使用這樣的公式
If(:Cuvette == 11,
:"850 nm"n / :"250 nm"n[Col Min(If(:Cuvette == 12, Row(), .)) + Row() - 1];
);
最終公式的類型在很大程度上取決於您的實際用例(資料是否「正確」排序,您是否正在尋找多個值比較等等)。