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
BHarris
Level VI

Convert all radians columns to degrees

I need a JSL loop that will search the column headers of a data table, and if the column header has "(rad)" in it, I'd like to multiply all of the values in that column by 180/pi, and then change "(rad)" to "(deg)" in the column header.

 

I have tried everything I can think of -- including chatGPT -- but so far no luck.

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Convert all radians columns to degrees

  1. Get all columns using << Get Column Names
  2. Set table to not update using << begin data update
  3. Loop over them using For Each
  4. Check if their name has (rad) in it using Contains / Starts With / Ends With
  5. Use For Each Row to loop over it and change the values to degrees
    1. I would hope there was better method for this, but I'm not sure if there really is unless you wish to create new columns and delete old as we shouldn't use << set each value which makes to extremely easy to do and read
    2. Apply Formula in place (added in JMP18) might work but it is difficult to understand how to use it
    3. Using matrix values is one good option but For Each Row will work always, always good idea to learn it
  6. Use substitute and << Set Name to rename the column
  7. let table update again using << end data update

 

Names Default To Here(1);

dt = New Table("Untitled",
	Add Rows(2),
	Compress File When Saved(1),
	New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([., .])),
	New Column("a (rad)", Numeric, "Continuous", Format("Best", 12), Set Values([1, .]))
);

colnames = dt << Get Column Names(Continuos, "String");

dt << Begin Data Update();
For Each({colname}, colnames,
	If(Contains(colname, "(rad)"),
		For Each Row(dt, Column(dt, colname)[] = Column(dt, colname)[] * 180 / Pi());
		Column(dt, colname) << Set Name(Substitute(colname, "(rad)", "(deg)"));
	)
);
dt << End Data Update();

Write();
-Jarmo

View solution in original post

3 REPLIES 3
jthi
Super User

Re: Convert all radians columns to degrees

  1. Get all columns using << Get Column Names
  2. Set table to not update using << begin data update
  3. Loop over them using For Each
  4. Check if their name has (rad) in it using Contains / Starts With / Ends With
  5. Use For Each Row to loop over it and change the values to degrees
    1. I would hope there was better method for this, but I'm not sure if there really is unless you wish to create new columns and delete old as we shouldn't use << set each value which makes to extremely easy to do and read
    2. Apply Formula in place (added in JMP18) might work but it is difficult to understand how to use it
    3. Using matrix values is one good option but For Each Row will work always, always good idea to learn it
  6. Use substitute and << Set Name to rename the column
  7. let table update again using << end data update

 

Names Default To Here(1);

dt = New Table("Untitled",
	Add Rows(2),
	Compress File When Saved(1),
	New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([., .])),
	New Column("a (rad)", Numeric, "Continuous", Format("Best", 12), Set Values([1, .]))
);

colnames = dt << Get Column Names(Continuos, "String");

dt << Begin Data Update();
For Each({colname}, colnames,
	If(Contains(colname, "(rad)"),
		For Each Row(dt, Column(dt, colname)[] = Column(dt, colname)[] * 180 / Pi());
		Column(dt, colname) << Set Name(Substitute(colname, "(rad)", "(deg)"));
	)
);
dt << End Data Update();

Write();
-Jarmo
BHarris
Level VI

Re: Convert all radians columns to degrees

Always impressive, thank you!

 

A few follow-on questions:

  1. Why is "colname" in curly braces as the first argument of the for each loop?
  2. I was trying to do something like "dt:colname[rowIdx] = dt:colname[rowIdx] * 180/Pi()" -- how would JMP interpreting this line and why is it wrong?
  3. What does the "[]" do at the end of "Column(dt,colname)[]"?  Is there a section of the documentation that discusses this construct?  

Thank you again!

jthi
Super User

Re: Convert all radians columns to degrees

  1. It is the syntax for For Each. Conditional and Logical Functions
  2. Most likely JMP won't be able to interpret anything from that as it doesn't find colname column from your data table as colname just stores the value. I think you could use As Column instead of Column()[] and it might work
    As Column(dt, colname) = As Column(dt, colname) * 180 / Pi()
  3. [] is same as [Row()] and it is used to access current row when using Column(). Iterate on Rows in a Table
-Jarmo