cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
nguyendv06
Level I

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)),
)
);

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

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 ||") ) );"));
Jim

View solution in original post

2 REPLIES 2
txnelson
Super User

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 ||") ) );"));
Jim
nguyendv06
Level I

Re: Adding up all the columns with names stored in a list

Wow Jim, thank you,

This works perfect.