Subscribe Bookmark RSS Feed

How to summarize by variable column names

MuttonChops

Occasional Contributor

Joined:

Mar 30, 2017

I have a table with variable numeric, continuous column names.  I need to get the mean of each column, grouped by a different column.  The script below shows what I'm trying to do and it makes a generic table that works for this purpose.  I'm getting hung up on the "Mean(ColNames)" line because it can't seem to process that.  

 

If I replace "Mean(ColNames)" with "Mean(:Col2, :Col3)" or "Mean(:Name("Col2"), :Name("Col3"))" then it works but obviously doesn't meet my need of being able to use variable columns.  Am I close or way off?

 

 


// Make a table to work with nr = 20; nc = 3; dt = NewTable("Random_Table"); For(c=1, c<=nc, c++, dt << NewColumn("Col"||Char(c), Numeric, Continuous, Formula(RandomInteger(1, 10))); ); dt << AddRows(nr); Column(1) << Data Type(Character); //Get column names for the numeric continuous columns ColNames = dt << Get Column Names(Numeric, "Continuous"); dt << Summary( Group(:Col1), Mean(ColNames), statistics column name format( "column" ) );

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson

Super User

Joined:

Jun 22, 2012

Solution

Put and Eval() around the columns list and it will work

names default to here(1);
dt=open("$SAMPLE_DATA\big class.jmp");

ColNames = dt << Get Column Names(Numeric, "Continuous");

dt << Summary(
		Group(:sex),
		Mean(eval(ColNames)),
		statistics column name format( "column" )
);
Jim
1 REPLY
txnelson

Super User

Joined:

Jun 22, 2012

Solution

Put and Eval() around the columns list and it will work

names default to here(1);
dt=open("$SAMPLE_DATA\big class.jmp");

ColNames = dt << Get Column Names(Numeric, "Continuous");

dt << Summary(
		Group(:sex),
		Mean(eval(ColNames)),
		statistics column name format( "column" )
);
Jim