Share your ideas for the JMP Scripting Unsession at Discovery Summit by September 17th. We hope to see you there!
Choose Language Hide Translation Bar
Highlighted
Raquel
Level II

For Loop for Moving Data to New Columns

Is there an easier way, possibly with a For loop, to get data from multiple "cells" and move it to a new column? Otherwise, I was going to write the script similar to below:

 

 

dt<<New Column( "Demand Parameter", Character, "Nominal");
dt<<New Column( "Demand Parameter Value", Character, "Nominal");
Column(dt, "Demand Parameter")[9] = Column(dt, "Column 2")[7];
Column(dt, "Demand Parameter Value")[9] = Column(dt, "Column 2")[8];
Column(dt, "Demand Parameter")[10] = Column(dt, "Column 3")[7];
Column(dt, "Demand Parameter Value")[10] = Column(dt, "Column 3")[8];
 
//.... continue incrementing until finishing with the following
 
Column(dt, "Demand Parameter")[47] = Column(dt, "Column 40")[7];
Column(dt, "Demand Parameter Value")[47] = Column(dt, "Column 40")[8];

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Raquel
Level II

Re: For Loop for Moving Data to New Columns

Thanks for replying to my question. Originally "Column 2" wasn't the second column... and "Column 40" wasn't the 40th but I rearranged my srcpit so that it is. I couldn't get your script to work but I did decide to use transpose.  The following works as I intended. Thanks for the idea!

 

dt = Current Data Table();
collist = dt << get column names;
dt << Select Rows( {7,8} );
dt<<Transpose(
columns( eval(collist) ),
Source Label Column( "Label" ),
Transpose selected rows only( 1 ),
Output Table( "Transpose Demands" ));
dt2 = Data Table("Transpose Demands");
dt2 << Add Rows(7,0);
dt << Update( With( dt2 ));
Close(Data Table( "Transpose Demands" ), No Save);
dt << Delete Columns( "Label" );

View solution in original post

3 REPLIES 3
Highlighted
Craige_Hales
Staff (Retired)

Re: For Loop for Moving Data to New Columns

Approximately this:

dt[ 9::47, { "Demand Parameter", "Demand Parameter Value" } ] = dt[ 7::8, 2::40 ] ` ;

if "Column 2" is the second column... and "Column 40" is the 40th. Note the final, tiny, ` at the end that means Transpose. You could also list all the column names instead of 2::40. The transpose is needed to make the Left Hand Side and the Right Hand Side be the same shape, 39 rows and 2 columns.

See Data Table Subscripting blog post. 

Craige
Highlighted
Raquel
Level II

Re: For Loop for Moving Data to New Columns

Thanks for replying to my question. Originally "Column 2" wasn't the second column... and "Column 40" wasn't the 40th but I rearranged my srcpit so that it is. I couldn't get your script to work but I did decide to use transpose.  The following works as I intended. Thanks for the idea!

 

dt = Current Data Table();
collist = dt << get column names;
dt << Select Rows( {7,8} );
dt<<Transpose(
columns( eval(collist) ),
Source Label Column( "Label" ),
Transpose selected rows only( 1 ),
Output Table( "Transpose Demands" ));
dt2 = Data Table("Transpose Demands");
dt2 << Add Rows(7,0);
dt << Update( With( dt2 ));
Close(Data Table( "Transpose Demands" ), No Save);
dt << Delete Columns( "Label" );

View solution in original post

Highlighted
Craige_Hales
Staff (Retired)

Re: For Loop for Moving Data to New Columns

Glad you got a solution that works!

Craige
Article Labels

    There are no labels assigned to this post.