在 For 循环中创建公式列我有一个脚本可以计算不合格代码的不合格率,还可以为用户输入的日期范围内的每个代码创建控制图。 用户最多可以输入五个单独的代码,并且有超过 50 个代码可供选择。 创建了一个表,其中包含每个日期每个代码的总不合格数量、所有输入代码的不合格数量总和以及每天制造的总数量(每个代码日期的 NC 率 - 原始)。 然后,我通过循环迭代添加到表中,以添加一列来计算每个代码的不一致率,然后一秒钟以百分比形式显示比率(NC Rate By Date Per Code - Complete)。现在所有的利率列最终都是相同的,百分比列也是一样的,尽管不是正确的利率百分比版本。
该循环基本上与在脚本末尾创建的循环相同,用于汇总整个日期范围内的不合格数据(每个代码的整体 NC 率 - 原始,每个代码的整体 NC 率 - 完整)计算正确但我不能告诉你出了什么问题。
我附上了 metnioned 表和脚本部分的屏幕截图(代码 = 所有输入代码的字符串列表,SD 和 NR 是表引用)
//NC Rate By Date Per Code Section
//Calculate NC rates and percentages for entered codes if two or more codes are entered
If(NItems(codes)>=2,
For(c=2, c<=NItems(codes)+1, c++,
//Add NC Rate column for each code
NR<<NEW column="">=2,
For(c=2, c<=NItems(codes)+1, c++,
//Add NC Rate column for each code
SD<<NEW column=""></NEW></NEW>回复:在 For 循环中创建公式列感谢您的额外解释!
回复:在 For 循环中创建公式列@rfeick ,
下面的脚本有望帮助您理解,从 JMP14 开始,在公式列中使用变量会产生错误结果,因为当变量值发生变化时,公式(及其值)也会发生变化。
关于脚本中的最后两个公式:
按照说明运行下面的脚本。 它演示了您的表格公式中发生的情况,以及因此产生的值。 此脚本还包括一种使用表达式将公式中的变量替换为变量值的方法。我通常使用 Set Each Value(),因此如果源列被删除,该列将保持不变。另请注意,此脚本使用C列号。 如果我想嵌入公式,我建议创建一个新变量cnme = Column(c) << 获取名称,然后使用 Column(cnme) 和表达式。 然后,如果表格被重组等,则公式符合预期(完整)。 下面显示的脚本已附加。
Names Default to Here(1);
dt = Open("$Sample_Data/Big Class.jmp");
//will create formula columns for height and weight using a relative column number , c
//c will change, simulating a loop.
//highlight the blocks of code and follow the instructions
//--- Block 0: run this to embed the log window
_cw = Current Window();
_ifb = _cw << Find( If Box( 1 ) );
wait(0);
_ifb << Set( 1 );
//--- end Block 0
//--- Block 1: run it and look at the look at the embedded log
c=4; //height
dt << New Column( (column(c) << get name) ||" per age", Numeric, Continuous, Formula(Round(column(c)[] /:age,2) ) );
nc1 = ncol(dt);
show( c, nc1, (column(nc1) << get formula), column(nc1)[1::5]);
//--- end Block 1
//--- Block 2: run it and look at the look at the embedded log
c=5; //weight
dt << rerun formulas;
nc1 = ncol(dt);
show( c, nc1, (column(nc1) << get formula), column(nc1)[1::5]);
//--- end Block 2 See the log, the formula is the same, but c has changed so the formula is now using column 5 (c=5)
// for its calculation
//--- Block 3: run it and look at the look at the log
c=5; //height
dt << New Column( (column(c) << get name) ||" per age", Numeric, Continuous, Formula(Round(column(c)[] /:age,2) ) );
nc2 = ncol(dt);
show( c, nc2, (column(nc2) << get formula), column(nc2)[1::5]); //same formula as nc1
show(column(nc1)[1::5], column(nc2)[1::5]);
//--- end Block 3
//will use formula columns for weight and height using expressions column number
//--- Block 4: run it and look at the look at the log
c=4; //height
Eval ( Eval Expr(
dt << New Column( (column(c) << get name) ||" per age using expr", Numeric, Continuous, Formula(Round(column(expr(c))[] /:age,2) ) )
));
nc3 = ncol(dt);
show( c, nc3, (column(nc3) << get formula), column(nc3)[1::5]);
//--- end Block 4 - note the formula now has the exact column number, it no longer relies on "c"
//--- Block 5: run it and look at the look at the log
c=5; //height
Eval(EvalExpr(
dt << New Column( (column(c) << get name) ||" per age using expr", Numeric, Continuous, Formula(Round(column(Expr(c))[] /:age,2) ) );
));
nc4 = ncol(dt);
show( c, nc4, (column(nc4) << get formula), column(nc4)[1::5]); //not the same formula as nc3,
show(column(nc3)[1::5], column(nc4)[1::5]);
//--- end Block 5
回复:在 For 循环中创建公式列@gzmorgan0 , 感谢您的解释。 我尝试了“设置每一行”方法,它解决了我在计算每天的不合格率时遇到的问题。 我仍然感到困惑的一点是,同一个理论如何在剧本的一个部分起作用,而在另一部分却不起作用?回复:在 For 循环中创建公式列@rfeick ,
最后两列的公式与在 for 循环中创建的公式不同。 最后两列引用实际列来进行计算。
您应该查看由您的 for 循环规范创建的几个列公式(见下文)
NR<<NEW column=""></NEW>
该公式将如下所示,它不会有 c 的值,因此您得到的结果将是 c 的最终值的结果。
<SPAN class="token function">Round</SPAN><SPAN class="token punctuation">(</SPAN><SPAN class="token punctuation">(</SPAN><SPAN class="token function">Column</SPAN><SPAN class="token punctuation">(</SPAN>c<SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">[</SPAN><SPAN class="token punctuation">]</SPAN> <SPAN class="token operator">/</SPAN> <SPAN class="token punctuation">:</SPAN>Manufactured Qty<SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">,</SPAN> <SPAN class="token number">4</SPAN><SPAN class="token punctuation">)</SPAN><CODE>
如果您保存此表并稍后在新的 JMP 会话中打开它并重新运行公式,那么这些列将为空,JMP 将不知道 c 的值。
您要么需要修改公式以使用替换 c 值的表达式,要么更改脚本以存储计算值而不是公式。 下面的代码演示了如何使用 Set Each Value() 来存储值而不是公式。
tcol = NR<<NEW column=""></NEW>