Subscribe Bookmark RSS Feed

Summary of Unknown Number of Columns

robot

Community Trekker

Joined:

Feb 27, 2012

Hi,

I would like to create a summary of all columns in a generic data table, where the number of columns are unknown.

Below is an example for a known data table.  Any suggestions for an unknown number of columns?

Thanks,

Rob

dt = Open( "$SAMPLE_DATA/Big Class.JMP" );

dtsum1 = dt << Summary( N( :name ), N( :age ), N( :sex ), N( :height ), N( :weight ) );

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

Summary accepts a list of columns as an argument. So this oneliner works too!

dt = Open( "$SAMPLE_DATA/Big Class.JMP" );

dt<<Summary(N(dt<<get column names));

5 REPLIES
pmroz

Super User

Joined:

Jun 23, 2011

There's probably a slicker way, but in situations like this I create dynamic code and then execute it:

dt = Open( "$SAMPLE_DATA/Big Class.JMP" );

dt_cols = dt << get column names(string);

for (i = 1, i <= nitems(dt_cols), i++,

    if (i == 1,

        n_expr = "dtsum1 = dt << Summary( N(:name(\!"" ||

                  dt_cols[i] || "\!"))";

        ,

        n_expr = n_expr || ", N(:name(\!"" || dt_cols[i] || "\!"))";

    );

);

n_expr = n_expr || ")";

eval(parse(n_expr));

adamrauk

Community Trekker

Joined:

Mar 5, 2012

I suggest building an expression and then inserting references to each column:

dt = Open( "$SAMPLE_DATA/Big Class.JMP" );

summaryexpr=Expr(Summary());

for(i=1,i<=ncol(dt),i++,

  insertinto(summaryexpr,insert(expr(N()),column(i)))

);

eval(substitute(expr(dt<<command),expr(command),summaryexpr));

Unless you are switching between data tables, you could simplify the last line by simply executing 'summaryexpr' rather than adding it as a command to 'dt'.

robot

Community Trekker

Joined:

Feb 27, 2012

Both solutions work great.  Thanks!

Solution

Summary accepts a list of columns as an argument. So this oneliner works too!

dt = Open( "$SAMPLE_DATA/Big Class.JMP" );

dt<<Summary(N(dt<<get column names));

robot

Community Trekker

Joined:

Feb 27, 2012

Very elegant.  Thanks!