cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
hsjoyner
Level I

JMP script for average column

I've a VERY novice JMP script writer trying to write a command to average a set of columns into a new column with the averages.  This is equivalent to highlighting the columns, then selecting New Formula Column > Combine > Average.  However, I can't find anything in the script library or online that lets me do this.  Anyone have any suggestions?

 

To add an additional complication, I want the script to take columns 2-11, 12-21, 22-31, etc. and average each column group.  The columns do not have set names, so I can't just put the column names into the script (it's not possible to make the names consistent because the column names are sample names and I need to keep them that way).  The number of column groups in each JMP file is not consistent because the number of samples in each JMP file can vary widely.  Is there a simple way to do this?

 

I'm using JMP 17.0.0 on a Mac if that's helpful.  Any help is appreciated!

 

4 REPLIES 4
StarfruitBob
Level VI

Re: JMP script for average column

Hello @hsjoyner,

 

Have you investigated the column() function? Instead of using a column name, you can use it's index.  This could be useful in a for() loop.

StarfruitBob_0-1680303365358.png

 

Learning every day!
hogi
Level XII

Re: JMP script for average column

If the number of column is fixed, you can hard-code the means,

if the "etc." is variable, you probably need some Insert Into and Eval(Substitute()):

 

dt = Open( "$SAMPLE_DATA/Semiconductor Capability.jmp" );
offset = 5;
nc = N Col( dt );
length = 10;
groups = Floor((nc - offset) / length);
For( group = 1, group <= groups, group++,
	myFormula = Expr( Mean() );
	For( i = 1, i <= length, i++,
		Insert Into( myFormula, NameExpr(As Column( offset + (group-1)*length +i ) ))
	);
	myName = "average" || Char( group );
	Eval(Substitute(Expr(new Column(__myName__,Formula(__myFormula__))),Expr(__myFormula__),NameExpr(myFormula),Expr(__myName__),NameExpr(myName) ));
);
txnelson
Super User

Re: JMP script for average column

Here is a script that I hope creates what you are looking for.  It creates new columns that average groups of 10 columns for an undetermined number of columns.

txnelson_0-1680334635093.png

names default to here(1);

// Create an example data table to use for illustration of how to 
// create the sets of average columns
dt  =
// Open Data Table: semiconductor capability.jmp
// → Data Table( "semiconductor capability" )
Open( "$SAMPLE_DATA/semiconductor capability.jmp" );

// Delete the first 3 columns to match the description
// of the suggested data table structure
dt << delete columns({"lot_id","wafer","wafer ID in lot ID"});

// Create new columns that have the groups of 10 columns
startingNCol = N Cols(dt);
start = 2;
groupSize = 10;
while(start+groupSize<=startingNCol,
	eval(substitute(expr(
		dt << New Column("Average " || char( start ) || "-" || char(start + groupSize),
			set each value(Mean(current data table()[row(),index(theStart,theEnd)])));
		),
		expr(theStart), start, expr(theEnd), start+groupSize
	));
	start=start+groupSize;
);

 

Jim
hogi
Level XII

Re: JMP script for average column

Mean(current data table()[row(),index(theStart,theEnd)])

wow, very elegant - much nicer than filling the mean manually with the column references!