cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • DownloadSemiconductor Toolkit: Tools to create wafer maps, add wafer geometry to graphics, explore die defects & compare wafers.
  • Discovery Summit 2026: Early User Edition - September 23-24.Register. It's free.
  • See how to use JMP Pro to analyze and predict using entire data set rather than selected points in a curve. Register for Nov. 6 Mastering JMP.

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
pauldeen
Level VI

How to create an average column with JSL based on random columns

I want to make a column list based on some selection criteria. Column names would not be known in advance. Then I use that column list to make a formula column that takes the mean (or sd or number) of those columns.

Simple example script which takes all numeric columns:

 

dt = Open( "$SAMPLE_DATA/Cars.jmp" );
ColList = {};
For(i=1, i<=NCols(), i++,
	If(Column(i)<<GetDataType=="Numeric", InsertInto(ColList, Column(i)))
);
MeanExpr = NameExpr(Mean());
For(i=1, i<=NItems(ColList), i++,
	InsertInto(MeanExpr, ColList[i])
);
Eval(
	EvalExpr(
		dt << NewColumn("Average of numerics",
			Formula(
				Expr(NameExpr(MeanExpr))
			)
		)
	)
);

MeanExpr2 = NameExpr(Mean());
For(i=1, i<=NItems(ColList), i++,
	InsertInto(MeanExpr2, NameExpr(AsColumn(ColList[i])))
);
Eval(
	EvalExpr(
		dt << NewColumn("Average of numerics 2",
			Formula(
				Expr(NameExpr(MeanExpr2))
			)
		)
	)
);

 

 

Notice that ColList is a valid list of columns. When this gets inserted into the first column formula, the preview in the formula editor shows correct evaluation but the column remains empty. Apparently we need to insert scalars, list or matrix into the Mean() function (but the preview will work regardless).

pauldeen_0-1788272598908.png

The second formula column adds a AsColumn() around the column reference...which is a bit counter intuitive as it is already a column. But now it creates a list, scalar or matrix and the mean() formula will work.

It feels like i'm making this way to complicated. Is there a simpler way to procedurally generate such a summary column?

1 REPLY 1
jthi
Super User

Re: How to create an average column with JSL based on random columns

Usually in simple cases like this I use Substitute to replace list with some other expression

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Cars.jmp");
collist = dt << Get Column Names("Numeric", "String");

colrefs = Transform Each({colname}, collist,
	Name Expr(As Column(dt, colname))
);

mexpr = Substitute(colrefs, Expr(List), Expr(Mean));

Eval(EvalExpr(
	dt << New Column("AVG", Numeric, Continuous, Formula(
		Expr(Name Expr(mexpr))
	));	
));

Write();
-Jarmo

Recommended Articles