我有一個包含很多列(> 100)的表。 我正在嘗試創建一個列來計算一行中大於 1 的列數。 我將如何編寫一個公式來做到這一點? 下面是示例表的小圖。
如果這只是一個我可以放入表格中的公式而不是一個獨立的 JSL 腳本,那就更好了。
這是一種方法:
NamesDefaultToHere(1);
// Build a table
nr = 20; // Number of rows
nc = 10; // Number of columns
dt = AsTable(J(nr, nc, RandomNormal(1, 1)));
dt << setName("Above and Below One");
// Find out what columns are in the table
cols = dt << getColumnNames("String");
// Build an expression to be used as the formula:
// Sum the results of an 'if' clause for each column that exists
form = Expr(Add());
for(c=1, c<=NItems(cols), c++,
ifClause = Expr(If(columnTBD > 1, 1, 0));
SubstituteInto(ifClause, Expr(columnTBD), Parse(":"||cols[c]));
InsertInto(form, NameExpr(ifClause));
);
// Inspect the resulting formula
Print(form);
// Add the formula column
fc = dt << NewColumn("Count Above One", Numeric, Ordinal, Formula(NameExpr(form)));