Subscribe Bookmark RSS Feed

Editing a formula in JMP script

akrishna39

Occasional Contributor

Joined:

Oct 22, 2017

Hi,

I have a very long formula stored in a variable named f. Now, I have to create a new column where I paste the formula b0 + b1*f. If I do it the following way, it works, but when I try to see the formula by right clicking on the column, I see " b0 + b1*f " as my formula. I wanted to see the expression of f stored in 'f' in the formula and not just the letter 'f'. Please help. Thanks!!

 

test_data<<NewColumn("response_hat");
Column( Data Table( "test_data" ), "response_hat" ) << Set Formula( Name Expr( b0 + b1*f ) );

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
ih

Community Trekker

Joined:

Sep 30, 2016

Solution

Now I understand and yes that is a pain.  The easiest way is probably to convert them back to strings as in h, below.  You can also do it with expressions, as in j.

dt = Open("$SAMPLE_DATA/iris.jmp");

f = Expr( 1 + 2 );
g = Expr( 1 + 2 + 3 );
h = parse(char(name expr(f)) || " + " || char(name expr(g)));
i = Expr( Expr(Name Expr(f)) + Expr(Name Expr(g)));
j = Eval Expr(i);

dt << New Column( "new", formula( name expr( f ) ) );
Column( dt, "new" ) << Set Formula( name expr( g ) );
dt << New Column( "from string", formula( name expr( h ) ) );
dt << New Column( "from expression", formula( name expr( j ) ) );
ian_jmp

Staff

Joined:

Jun 23, 2011

Solution

Please find an alternative:

Names default to here(1);

f = Expr( 1 + 2 );
g = Expr( 1 + 2 + 3 );
h = Substitute(Expr(A + B), Expr(A), NameExpr(f), Expr(B), NameExpr(g));

dt = NewTable("Sum", NewColumn("A+B", Numeric, Continuous, Formula(NameExpr(h))), AddRows(3));
7 REPLIES
ih

Community Trekker

Joined:

Sep 30, 2016

The constants b0 and b1 inside Name Expr() are causing the problem.  That function needs a variable that contains an expression rather than an expression.  Try putting the entire formula inside f and then call Name Expr( f ):

 

dt = Open("$SAMPLE_DATA/iris.jmp");

f = Expr( 1 + 2 );
g = Expr( 1 + 2 + 3 );

dt << New Column( "new", formula( name expr( f ) ) );
Column( dt, "new" ) << Set Formula( name expr( g ) );
akrishna39

Occasional Contributor

Joined:

Oct 22, 2017

Thanks ih. But my question is how to put entire formula inside f? For example, in your script, if you wanted to create a new column having the formula f+g, how will you do it? And if I right click on that new column, it should show me "1 + 2 + 1 + 2 + 3", instead of "f + g"

ih

Community Trekker

Joined:

Sep 30, 2016

Solution

Now I understand and yes that is a pain.  The easiest way is probably to convert them back to strings as in h, below.  You can also do it with expressions, as in j.

dt = Open("$SAMPLE_DATA/iris.jmp");

f = Expr( 1 + 2 );
g = Expr( 1 + 2 + 3 );
h = parse(char(name expr(f)) || " + " || char(name expr(g)));
i = Expr( Expr(Name Expr(f)) + Expr(Name Expr(g)));
j = Eval Expr(i);

dt << New Column( "new", formula( name expr( f ) ) );
Column( dt, "new" ) << Set Formula( name expr( g ) );
dt << New Column( "from string", formula( name expr( h ) ) );
dt << New Column( "from expression", formula( name expr( j ) ) );
ian_jmp

Staff

Joined:

Jun 23, 2011

See below.

ian_jmp

Staff

Joined:

Jun 23, 2011

Solution

Please find an alternative:

Names default to here(1);

f = Expr( 1 + 2 );
g = Expr( 1 + 2 + 3 );
h = Substitute(Expr(A + B), Expr(A), NameExpr(f), Expr(B), NameExpr(g));

dt = NewTable("Sum", NewColumn("A+B", Numeric, Continuous, Formula(NameExpr(h))), AddRows(3));
akrishna39

Occasional Contributor

Joined:

Oct 22, 2017

Awesome!! Thanks ian !!

akrishna39

Occasional Contributor

Joined:

Oct 22, 2017

This is cool!! Thanks ih !!! :)