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
Stas
Level I

Create/ add new columns using veribles

Hey guys, I apologize for the confusing title. I wasn't sure how to phrase it.
My question is: I have a code in which I'm adding new columns using long conditions.

The conditions are the same for all the columns, except to one parameter that changes.
Is there a way, instead writing each time the full code and the conditions with the new parameter value, create a template (like a function) that I will use a variable instead of the parameter to which I'll send each time the needed value for the conditions of the new column?

5 REPLIES 5
hogi
Level XII

Re: Create/ add new columns using veribles

Since JMP 18 the parallel creation of new columns got very easy - just have a look at Discussions/Formula/ - feature #3.

In short:
Select the columns which you want to use as input, then right click on a column header and click
new formula columnhogi_0-1735849178996.png

As a last step, key in your formula and click on hogi_2-1735849401521.png

... maybe, with a trick one can even use different parameters for the different columns?

hogi
Level XII

Re: Create/ add new columns using veribles

If you want to use exactly the same formula again and again via the new formula right click menu, you can create your own "custom function" and enable the Transform Category option to make it show up in the menu.
After a restart the code has to be executed again, so include it in an AddIn or in your startup scripts.

 

Names Default To Here( 1 );
myAdd =
New Custom Function(
	"custom",
	"Add",
	Function( {x, y = 1},
		x + y - 1
	)
);
myAdd << Transform Category( 1 );

from scripting Index:

hogi_3-1735849930634.png


But this approach is very "static". To get the possibility to change a parameter, you have to vote here:
New Formula Column menu: user-Defined mini GUIs 

jthi
Super User

Re: Create/ add new columns using veribles

Here is one example of function which will add a number. It is missing some checks to be a "good" function but does provide the necessary idea that values which might change (column name and number to add in this case) must be evaluated (see Insert one expression into another... I mentioned earlier)

Names Default To Here(1); 

dt = open("$SAMPLE_DATA/Big Class.jmp");

cols = {"height", "weight"};

add_num_to_col = Function({dt, colname, num_to_add}, {Default Local},
	new_col = Eval(EvalExpr(
		dt << New Column(colname || " add " || char(num_to_add), Numeric, Continuous, Formula(
			Expr(NameExpr(AsColumn(dt, colname))) + Expr(num_to_add)
		));
	));
	
	return(new_col);
);

col1 = add_num_to_col(dt, "height", 10);
col2 = add_num_to_col(dt, "height", 20);
col3 = add_num_to_col(dt, "weight", 5);

Write();

Sometimes using Eval(Substitute()) is cleaner and easier to understand solution and it could look something like this

add_num_to_col = Function({dt, colname, num_to_add}, {Default Local},
	new_col = Eval(Substitute(
		Expr(dt << New Column(colname || " add " || char(num_to_add), Numeric, Continuous, Formula(
			_colname_ + _num_to_add_
		))),
		Expr(_colname_), Name Expr(AsColumn(dt, colname)),
		Expr(_num_to_add_), num_to_add
	));
	
	return(new_col);
);
-Jarmo
hogi
Level XII

Re: Create/ add new columns using veribles

A combination of the previous approaches - with a little twist:

  • create a custom function *)
  • add it to a formula category
  • right click on a column header and click new formula column / hogi_0-1735893323385.png
  • use the new formula 
  • ... and enter the parameter manually:

hogi_1-1735893371044.png

 

*)

Add Custom Functions(
	New Custom Function(
		"custom",
		"add++",
		Function( {column, num_to_add},
			{Default Local},
			column,
			num_to_add
		),
		<<Formula Category( "NumberStuff" )
	)
);

The benefit: the user can enter specific parameters for every new column.

The disadvantage: no batch mode like in the previous posts.

kind of disadvantage: JMP doesn't resolve/replace the formula, it keeps it as add++(...).
   To keep it alive the formula definition has to be executed again after a restart of JMP - and maybe:  shared with colleagues.