sum 不接受 list 的格式,所以只是想知道如何做到这一点? 谢谢!
回复:使用循环对所有列求和我应该补充一点,脚本像这样初始化 dt 和 N:
dt = Current Data Table();
N = N Col( dt );
使用循环对所有列求和大家好,构建一个相当冗长的脚本,我不知道给定表中存在哪些列,因为它正在从不同的数据集创建多个表。 创建的汇总表包含 50 列以上,有时超过 70 列。 我需要对这些列中的所有值求和,但不能通过 :Name("Column Name") 指定总和,以防某些类别存在/缺失。 这是我正在尝试使用的循环:
Current Data Table() << New Column( "Total Die",
formula(
For( j = 1, j <= N, j++,
cols = Column( dt, j ) << getname()
);
Sum( cols );
)
);
sum 不接受 list 的格式,所以只是想知道如何做到这一点? 谢谢!
脚本回复:使用循环对所有列求和如果我理解正确,那么与其构建循环,不如利用“Tables > Summary”并让 JMP 完成工作:
NamesDefaultToHere(1);
// Get some data
dt = Open("$SAMPLE_DATA/Semiconductor Capability.jmp");
// Get all the numeric columns
cols = dt << getColumnNames("Numeric");
// Get all the column means
dt2 = dt << Summary(Mean(cols));
这是否更好可能取决于您的 JSL 在做什么,但通常使用 JMP 平台是好的,尤其是涉及数字时,即使是简单的数字。
回复:使用循环对所有列求和扩展一下您的知识,我认为处理该问题的最简单方法是不创建公式列,而是创建列,然后使用 JSL 的矩阵功能来执行魔术。
Names Default To Here( 1 );
dt = Current Data Table();
N = N Col( dt );
dt << New Column( "new" );
For Each Row(
dt:new = Sum( dt[Row(), index(1,ncols(dt)-1)] )
);
回复:使用循环对所有列求和完全矢量化计算。
Names Default To Here( 1 );
dt = Current Data Table();
n = N Col( dt );
vals = (dt << Get As Matrix) * J( n, 1, 1);
dt << New Column( "new", "Numeric", "Continuous",
Values( vals )
);
回复:使用循环对所有列求和糟糕! 抱歉 - 看起来您实际上想要按行对各列求和。 看,例如.
或者,如果您确实想要一个公式:
NamesDefaultToHere(1);
// How many columns in the table?
nc = RandomInteger(2, 20);
// Build the table with some random data
dt = NewTable("Sum Columns", << addMultipleColumns("Col ", nc, Numeric));
for(c=1, c<=nc, c++, Column(dt, c) << setFormula(RandomNormal()));
dt << addRows(10);
dt << runFormulas;
// Start here with the current data table
dt2 = CurrentDataTable();
cols2 = dt2 << getColumnNames("Numeric");
// Add the row-wise sum as a new formula column
afc = Expr(dt2 << newColumn("Sum over Columns", Numeric, Continuous, Formula(Sum(colsTBD))));
SubstituteInto(afc, Expr(colsTBD), Eval(cols2));
afc;
(这篇文章最初是不完整的)。