根据条件进行行间计算嗨,我是 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];
);
最终公式的类型在很大程度上取决于您的实际用例(数据是否“正确”排序,您是否正在寻找多个值比较等等)。