Subscribe Bookmark RSS Feed

assigning colum name as value in another dt

I hope this example is self-explanatory. I created it as a self-contained example to duplicate problem I've having in a more complex real-life model. I'm trying to iteratively run bivariate analysis on multiple columns of a table and capture the output with the column name as a value...

2 REPLIES
mattf

Community Trekker

Joined:

Jun 23, 2011

// Set up the example
// Create a table with 3 columns
dt = New Table( "My Table",
New Column( "Week" ),
New Column( "ValA" ),
New Column( "ValB" )
);
colW = Column( "Week" );
colA = Column( "ValA" );
colB = Column( "ValB" );
dt << addrows( 200 );

// Populate sample data into table
For( i = 1, i < 201, i++,
:Week = i;
:ValA = Random Normal();
:ValB = Random Normal();
);
////////////////////////////////////////////////
collist = dt << get column names();
/* iterate through columns 2...n and do bivariate analysis on each*/
For( i = 2, i <= N Col( dt ), i++,
y = Column( dt, i ) << GetAsMatrix;
x = Column( dt, "Week" ) << GetAsMatrix;
coef = Spline Coef( x, y, 100 );

//get the name of current column
c = collist;

//assign coefficient values to a data table
dtcoef = As Table( coef );
Column( dtcoef, "Col1" ) << Set Name( "WEEK" );
Column( dtcoef, "Col2" ) << Set Name( "A" );
Column( dtcoef, "Col3" ) << Set Name( "B" );
Column( dtcoef, "Col4" ) << Set Name( "C" );
Column( dtcoef, "Col5" ) << Set Name( "D" );

// add a column for the Item name
pcol = dtcoef << New Column( "ITEM", Character, Width( 15 ) );

// set the Item value to the original column name
dtcoef:pcol = Char(collist);
);
MattF - sorry I can't see your solution, but I did figure it out on my own. Once I explicitly cast the column name to CHAR, it works.