在 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>