Subscribe Bookmark RSS Feed

How to use columnlist to stack a lot of columns

engr

Community Trekker

Joined:

Jun 23, 2011

Hi all, I want to read out the column names and save them as a column list .... using this list can I stack these multiple columns in a single command instead of manual stacking like the code shown below ?

dt_L5 << Stack(

    columns(

        :Name( "MISC_L5_Test230_2(1)" ),

        :Name( "MISC_L5_Test230_2(2)" ),

        :Name( "MISC_L5_Test230_2(3)" ),

        :Name( "MISC_L5_Test230_2(4)" ),

        :Name( "MISC_L5_Test230_2(5)" ),

        :Name( "MISC_L5_Test230_2(6)" ),

        :Name( "MISC_L5_Test230_2(7)" ),

        :Name( "MISC_L5_Test230_2(8)" ),

          .

          .

          .

          .

       :Name( "MISC_L5_Test230_2(125)" )

),

    Source Label Column( "Label" ),

    Stacked Data Column( "Data" ),

    Output Table( "L5" )

);

I want to do some thing like this :

clist=dt_L5<<Get Column Names;

ncols = N Items(clist);

dt_L5 << Stack(

    columns(

     for(i=1,i<=ncols,i++,

         name_list = clist;

         /* HOW SHOULD I CALL THE ":Name("name_list") " here ?? */

     );

    Source Label Column( "Label" ),

    Stacked Data Column( "Data" ),

    Output Table( "L5" )

);

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

Either contitionally like this:

tlist={};

for(i=1,i<=ncols,i++,

    if(i>3,

        insertinto(tlist,collist[i]);

    );

);

or without condition but a modified initial i:

tlist={};

for(i=4,i<=ncols,i++,

        insertinto(tlist,collist[i]);

);


9 REPLIES
ms

Super User

Joined:

Jun 23, 2011

Stack takes a list as argument so it's can be done in much simpler way here.

collist = dt1_L5 << get column names;

dt1_L0 << stack(

          columns( (collist) ),

          Source Label Column( "Label" ),

          Stacked Data Column( "Data" ),

          Output Table( "L5" )

);

Avoid for-loops inside platform commands. Btw, I tried to submit a reply to your previous question, but it had been deleted before I hit "Add Reply". Probably you found a solution already, but here's my take on that. Again, for-loops inside platforms may not always work as some commands may not evaluate the argument (e.g a quoted string is required).

monthlist = {4, 5, 7, 9, 11, 12};

wind = New Window( "Bivariate",

          dt1_L0 << Bivariate( Y( :Data ), X( :Price ), By( :MONTH ) )

);

For( i = 1, i <= N Items( monthlist ), i++,

          wind[1, i, Framebox( 1 )] << {Frame Size( 633, 249 ),

           Row Legend(:FLOORS,

                    Color Theme( "JMP Default" ),

                    Marker Theme( "Standard" ),

          )}

);

engr

Community Trekker

Joined:

Jun 23, 2011

Hi MS,

Thanks for the quick reply. I'm a newbie to JSL so I dont know my way across the language. Good to know that stack accepts list as an argument.

Can I expand the question further and ask ... how can we select only a few columns to be stacked from the list ? For example, I dont want to stack the first 3 columns. For this purpose, can I make a new list which will store the required column names (drops the first 3 column names from the collist) .

Yeah, I figured out a solution to my previous entry so I deleted it

Thanks so much!

engr

Community Trekker

Joined:

Jun 23, 2011

Something like this ?? ....

collist = dt1_L5 << get column names;


/*Intent here is to save a new list by dropping the first 3 from the prev list*/

ncols = N Items(collist);

for(i=1;i<=ncols;i++,

    if(i>3,

        tlist=collist;

    );

);

dt1_L0 << stack(

          columns( (tlist) ),

          Source Label Column( "Label" ),

          Stacked Data Column( "Data" ),

          Output Table( "L5" )

);

I dont even know if the "if" statement is correct here ... please excuse my ignorance

Solution

Either contitionally like this:

tlist={};

for(i=1,i<=ncols,i++,

    if(i>3,

        insertinto(tlist,collist[i]);

    );

);

or without condition but a modified initial i:

tlist={};

for(i=4,i<=ncols,i++,

        insertinto(tlist,collist[i]);

);


engr

Community Trekker

Joined:

Jun 23, 2011

Thanks once again! ... After the stack command I get the following message in log window

"Column not found in access or evaluation of 'Bad Argument'

Empty()"

And I do not see any stacked table output ... :-s I really appreciate your help!

engr

Community Trekker

Joined:

Jun 23, 2011

a search on this forum directed me to the answer ..... eval(list) has to be used.

collist = dt1_L5 << get column names;

dt1_L0 << stack(

          columns( eval(collist) ),

          Source Label Column( "Label" ),

          Stacked Data Column( "Data" ),

          Output Table( "L5" )

);

Thank you for your support!

ms

Super User

Joined:

Jun 23, 2011

Ok. For me it also works without eval(). JMP 9.0.3 for Mac.

This even works, i.e creating the list inside the columns()-argument. Can be convenient if you do not need the parameter later in the script. However, clearly stating variables makes the code easier to read and debug.

dt1_L0 << stack(

          columns( dt1_L0 << get column names ),

          Source Label Column( "Label" ),

          Stacked Data Column( "Data" ),

          Output Table( "L5" )

);

engr

Community Trekker

Joined:

Jun 23, 2011

Great! Thanks MS ... I have JMP8.0 maybe that's the problem

dabiziv

Community Member

Joined:

Jan 4, 2012

Hi,

does your final script looks something like this?


Clear Globals (); /* clears resident memory from previous runs*/
/* Open your file*/
dt=open(
     Pick File(
          "Select a *.csv file",
          Get Current Directory(),
          {"All JMP Files|jmp;jsl;jrn;csv;txt", "All Files|*"}
      )
     );
wait(0);

nc_dt = N Col (); /*gets number of columns in a file*/
wait(0);
clist = dt <<Get Column Names; /* gets colum names to be used in stacking*/
ncols = N Items(clist);
wait(0);
tlist={};
for(i=2,i<=ncols,i++,
    insertinto(tlist,clist);
    );
wait(0);
dt2 << Stack(
    columns(eval(tlist)),
    Source Label Column( "Label" ),
    Stacked Data Column( "Data" ),
    Output Table( "DT5" )
);