我想用公式 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 构建为两个字符串的连接,然后将其解析为表达式。 parse 函数的结果是一个表达式。</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] )
)
)
)
);