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;
(這篇文章最初是不完整的)。