我想新增帶有公式 B2/A2、C2/A2...的系列新欄位...如下例所示
我該如何實施它
謝謝

colname = dt << get column names( string, continuous );
nc = N Items( colname );
For( p = 1, p < nc, p++,
For( i = 1, i < nc, i++,
a = "Bin" || Char( p );
b = Char( a ) || Char( "(%)" );
If( Contains( colname[i], a ),
dt << New Column( b, formula( :colname[i] / :name( "Total" ) ) )
);
)
);
如果列名中有特殊字符,您可以在替換中使用 Parse( "As Column \!"" || colname[i] || "\!"" ) 。原始帖子嘗試使用 Name(),但它很難在腳本中使用,因為它只接受帶引號的字串。
好的。 我喜歡最後的表情處理和角色處理的混合搭配!
1 Eval(
2 Substitute(
3 Expr(
4 dt << New Column( colname[i] || "%",format("Percent",7,2),
5 formula( __col__ / :name( "Total" ) ) )
6 ),
7 Expr( __col__ ),
8 Parse( ":" || colname[i] )
9 )
10 )</CODE>第 4 行和第 5 行是作為表達式進行操作的 JSL; __col__ 是一個佔位符。</P><P>第 3 行和第 6 行分別是<EM>引用</EM>表達式,因此它不會嘗試運行。</P><P>第 7 行也標識了要替換的位<EM>引</EM>所以它不執行。</P><P>第 8 行是重置值。 這很有趣,因為它實際上將一些 JSL 建構成兩個字串的串聯,然後將其解析為表達式。 解析函數的結果是一個表達式。</P><P>第 2 行和第 9 行分別是<EM>替代(來源、模式、替換)</EM>功能。 它傳回一個由三個表達式參數建構的表達式。</P><P>第 1 行和第 10 行計算表達式的值。</P></LINGO-BODY><LINGO-SUB id="lingo-sub-284389" slang="en-US">回覆:如何使用 for 迴圈在行中新增列</LINGO-SUB><LINGO-BODY id="lingo-body-284389" slang="en-US"><P>你讓問題變得比需要的更複雜。以下是您的程式碼的修改,帶有一些註釋</P>
<PRE><CODE class=" language-jsl">Names Default To Here( 1 ); // This line tells JMP to isolate the variable used to this script
// Set a variable to point to the current active data table
dt = Current Data Table();
// Get the column names
colname = dt << get column names( string, continuous );
// Determine the number of columns to loop through
nc = N Items( colname );
// Loop across all of the columns in the colname list
// Your original code was For( i = 1, i < nc, i++,
// with "i < nc" the last colname would not be processed
For( i = 1, i <= nc, i++,
// If this is a column name that starts with "Bin"
// a new % column needs to be added
If( Left( colname[i], 3 ) == "Bin",
// Create the new column
Eval(
Substitute(
Expr(
dt << New Column( colname[i] || "%",format("Percent",7,2),
formula( __col__ / :name( "Total" ) ) )
),
Expr( __col__ ), Parse( ":" || colname[i] )
)
)
)
);