跨列条件计数我有一个包含相当多列(> 100)的表。 我正在尝试创建一个列来计算一行中大于 1 的列数。 我该如何写一个公式来做到这一点? 下面是示例表的小图片。
如果这只是一个可以放入表中的公式而不是独立的 JSL 脚本,那就更好了。
自动化和脚本编写回复:跨列的条件计数此公式希望您的“计算”列成为数据表中的最后一个列,并且必须在添加公式之前创建它。 此外,这不会像普通公式一样更新,因此您必须打开公式并按“应用”:
Sum(Current Data Table()[Row(), Index(1, N Cols(Current Data Table()) - 1)] > 1)
回复:跨列的条件计数这是一种方法:
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)));回复:跨列条件计数为了方便数据更新,您可以将其中一个输入列(此处为高度)作为虚拟列添加到列公式中。然后 JMP 将监视此列。要触发更新,只需编辑此列中的值即可。
另外,你可以把数据表的“访问”和列定义放入常量中。
Open( "$SAMPLE_DATA/Big Class.jmp" );
New Column( "values > 60",
Formula(
As Constant(
dt = Current Data Table();
//cols = Index(4,5);
cols = {"height", "weight"};
);
:height;
Sum( dt[Row(), cols] > 60 );
)
);