Subscribe Bookmark RSS Feed

JLS- Divide Column by a Value

natalie_

Community Trekker

Joined:

Jan 6, 2016

Hi again,

I am writing some script which opens several csv files and joins them; it is working well!

In one column, I'd like to divide the values of another column by a value.  I just can't figure it out.  I can do it on the formula editor though, but I don't believe I can use that syntax while scripting.

    dt << New Column(".");  //just to create a blank column

    dt << New Column("Id(A/mm)");

 

    col = Column("Id(A/mm)");

    col << Set Formula(Column("Id(A)")/Wg);

    col << EvalFormula;

[code/]

1 ACCEPTED SOLUTION

Accepted Solutions
David_Burnham

Super User

Joined:

Jul 13, 2011

Solution

something like this maybe:

someNumbers = 1::10;

New Table("demo",

    Add Rows(10),

       New Column("Sequence", numeric, continuous, <<Set Values(someNumbers) )

);

col = New Column(".", numeric, continuous);

valueToDivideBy = 2;

col << Set Formula(:Sequence/valueToDivideBy);

// or ...

col = New Column("..", numeric, continuous);

col << Set Formula( AsColumn("Sequence")/2);

-Dave
4 REPLIES
David_Burnham

Super User

Joined:

Jul 13, 2011

Solution

something like this maybe:

someNumbers = 1::10;

New Table("demo",

    Add Rows(10),

       New Column("Sequence", numeric, continuous, <<Set Values(someNumbers) )

);

col = New Column(".", numeric, continuous);

valueToDivideBy = 2;

col << Set Formula(:Sequence/valueToDivideBy);

// or ...

col = New Column("..", numeric, continuous);

col << Set Formula( AsColumn("Sequence")/2);

-Dave
txnelson

Super User

Joined:

Jun 22, 2012

There is an issue with the first solution provided by David.  While the code will work, the formula created is:

     :Sequence/valueToDivideBy

Since "valueToDivideBy" is a scalar memory variable, that will go away once the JMP session is closed, or could change value during the course of the day's processing, the calculated value of "col" could become invalid or incorrect.  The second version of the formula would be preferred.  However, if you want to use the structure of the first solution, you could do a couple of things:

1. Change the Se Formula to:

     col << Set Formula(Eval(Parse(":Sequence/"||char(valueToDivideBy)));

2. Convert the formula to static values once the formula has been applied.  To do this, add the following line after the the col << Set Formula line

     col << delete property("formula");

Jim
David_Burnham

Super User

Joined:

Jul 13, 2011

yes, well spotted.  thanks

-Dave
natalie_

Community Trekker

Joined:

Jan 6, 2016

I chose to use David's second method.  I think I understand why his first method has an issue, but I was unable to make your suggested changes.  As I run the script, I can make all the new column, but the formulas are not working.

  col= new column("."); //add blank column

  col=New Column("Id(A/mm)",numeric,continuous);

  col<<Set Formula(Eval,Parse(":Id(A)/"||Char(Wg)));

  col<<delete property(":Id(A)/"||Char(Wg));