如何制作一个可以接受可变数量输入列的公式?大家好,
我编写了一个脚本,用于通过分组列将数据列居中:

目前它只支持一列居中,但我希望能够添加多列。有什么建议可以让公式更具动态性吗?目前我只能用这个来新建一列,然后手动添加其他列来居中。
Names Default To Here( 1 );
dt = current data table();
results = Column Dialog(src=ColList("Cols to be centered", Min Col(1)),trg = ColList("centered by", Min Col(1)));
for(i=1, i<= n items(eval(results[1])), i++,
ts=expr(dt << New Column( "cent col", "Numeric",Formula(Expr(Eval( results[1] )[i]) - Col Mean( Expr(Eval( results[1] )[i]), Expr(Eval( results[2] )[1]) ))));
eval(eval expr(TS));
ts=expr(dt:"cent col"n<<SET name=""></SET>
<P>谢谢您的意见!</P>
<BR />
<BR /><LINGO-LABS id="lingo-labs-869624" slang="en-US" mode="CREATE"><LINGO-LABEL>自动化和脚本</LINGO-LABEL></LINGO-LABS>回复:如何制作一个可以接受可变数量输入列的公式?“居中列”——这经常需要用到。关于如何居中(除或减)以及如何计算“参考”,有数百种组合:

您可以访问https://marketplace.jmp.com/appdetails/Normalization+GUI - 它会在 JMP 菜单中添加一个 GUI 来帮您完成这项工作。即使排除了行,它也同样有效。
如果你想自己设置,基本思路就是一步一步地操作表达式。
例如使用插入添加动态数量的分组列:
For Each( {groupByCol, idx}, currentGroupCols,
Insert Into( secondArgumentDirect, Name Expr( As Column( dt, groupByCol ) ) )
);回复:如何制作一个可以接受可变数量输入列的公式?您的意思是您希望添加更多分组列吗?
Names Default To Here(1);
dt = Current Data Table();
results = Column Dialog(
src=ColList("Cols to be centered", Min Col(1)),
trg = ColList("centered by", Min Col(1))
);
srcs = results["src"];
trgs = results["trg"];
For Each({src}, srcs,
colname = (src << get name) || " centered by";
mean_expr = Expr(Col Mean());
Insert Into(mean_expr, NameExpr(src));
For Each({trg}, trgs,
colname = colname || " " || (trg << get name);
Insert Into(mean_expr, NameExpr(trg));
);
Eval(Substitute(
Expr(dt << New Column(colname, "Numeric", "Continuous", Formula(
_col1_ - _mean_
));
),
Expr(_col1_), Name Expr(src),
Expr(_mean_), Name Expr(mean_expr),
));
);
您也可以选择让 JMP 通过使用“新公式列”并在之后更改名称来执行更多操作
Names Default To Here(1);
dt = Current Data Table();
results = Column Dialog(
src=ColList("Cols to be centered", Min Col(1)),
trg = ColList("centered by", Min Col(1))
);
srcs = results["src"];
trgs = results["trg"];
res = dt << New Formula Column(
Operation(Category("Distributional"), "Center"),
Columns(srcs),
Group By(trgs)
);回复:如何制作一个可以接受可变数量输入列的公式?谢谢@hogi 。我得把这个插件加进去。
啊,“插入”,这看起来就是票(因为@jthi也进入了)。
回复:如何制作一个可以接受可变数量输入列的公式?嘿@jthi ,
是的,它可以添加更多分组列,第一个脚本很完美!谢谢你的帮助!
回复:如何制作一个可以接受可变数量输入列的公式?一个选项是使用 Substitute 来用 Col Mean 使用公式中的列列表替换某些表达式。
这是您的数据的懒惰示例
Names Default To Here(1);
dt = Current Data Table();
results = Column Dialog(
src=ColList("Cols to be centered", Min Col(1)),
trg = ColList("centered by", Min Col(1))
);
srcs = results["src"];
trgs = results["trg"];
For Each({src}, srcs,
colname = (src << get name) || " centered by";
l = Insert(trgs, Name Expr(src), 1);
f = Substitute(l, Expr(List), Expr(Col Mean()));
Eval(Substitute(
Expr(dt << New Column(colname, "Numeric", "Continuous", Formula(
_col1_ - _mean_
));
),
Expr(_col1_), Name Expr(src),
Expr(_mean_), Name Expr(f),
));
);
只需注意一点:我将使用“新窗口”而不是“列对话框”来构建对话框,因为即使在相当简单的情况下它也提供了更大的灵活性。
回复:如何制作一个可以接受可变数量输入列的公式?感谢@jthi添加的选项。
是的,这是一个较旧的脚本,我还没有切换到使用新窗口,所以我现在已经改正了这个坏习惯!