- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Adding up all the columns with names stored in a list
I have create a list of column containing roughly the same name below:
Xlist = {};
Ylist = {};
for(i = 1, i < ncols(dt2), i++,
if(
contains(dt2:i<< get name, "XXX") > 0, insert into(Xlist, dt2:i<< get name),
contains(dt2:i<< get name, "YYY") > 0, insert into(Ylist, dt2:i<< get name),
)
);
I would like to create a new column which will give me a sum of the Xcolumn and Ycolumn. It is not working well for me at all. I must not be doing something right.... Please help.
dt2 << New Column( "Xsum", Numeric, Continuous, Format( "Best", 12 ),
Formula(
sum(eval(Xlist)),
)
);
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Adding up all the columns with names stored in a list
The formula section within a New Column is not parsed and evaluated as one might expect. I have found, that the best way to handle this, is to generate a literal string of the complete New Column phrase, exactly as you would want it if you manually typed it in, and then to execute that line. The code below does that
Names Default To Here( 1 );
dt2 = Current Data Table();
Xlist = "";
Ylist = "";
For( i = 1, i < N Cols( dt2 ), i++,
If(
Contains( column(dt2,i) << get name, "XXX" ) > 0, Xlist=Xlist || ":" || char(column(dt2,i) << get name) || ",",
Contains( column(dt2,i) << get name, "YYY" ) > 0, Ylist=Ylist || ":" || char(column(dt2,i) << get name) || ","
)
);
eval(parse("dt2 << New Column( \!"Xsum\!", Numeric, Continuous, Format( \!"Best\!", 12 ), Formula( Sum( "||Xlist ||") ) );"));
eval(parse("dt2 << New Column( \!"Ysum\!", Numeric, Continuous, Format( \!"Best\!", 12 ), Formula( Sum( "||Ylist ||") ) );"));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Adding up all the columns with names stored in a list
The formula section within a New Column is not parsed and evaluated as one might expect. I have found, that the best way to handle this, is to generate a literal string of the complete New Column phrase, exactly as you would want it if you manually typed it in, and then to execute that line. The code below does that
Names Default To Here( 1 );
dt2 = Current Data Table();
Xlist = "";
Ylist = "";
For( i = 1, i < N Cols( dt2 ), i++,
If(
Contains( column(dt2,i) << get name, "XXX" ) > 0, Xlist=Xlist || ":" || char(column(dt2,i) << get name) || ",",
Contains( column(dt2,i) << get name, "YYY" ) > 0, Ylist=Ylist || ":" || char(column(dt2,i) << get name) || ","
)
);
eval(parse("dt2 << New Column( \!"Xsum\!", Numeric, Continuous, Format( \!"Best\!", 12 ), Formula( Sum( "||Xlist ||") ) );"));
eval(parse("dt2 << New Column( \!"Ysum\!", Numeric, Continuous, Format( \!"Best\!", 12 ), Formula( Sum( "||Ylist ||") ) );"));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Adding up all the columns with names stored in a list
Wow Jim, thank you,
This works perfect.