- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Convert all radians columns to degrees
- Get all columns using << Get Column Names
- Set table to not update using << begin data update
- Loop over them using For Each
- Check if their name has (rad) in it using Contains / Starts With / Ends With
- Use For Each Row to loop over it and change the values to degrees
- 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
- Apply Formula in place (added in JMP18) might work but it is difficult to understand how to use it
- Using matrix values is one good option but For Each Row will work always, always good idea to learn it
- Use substitute and << Set Name to rename the column
- 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
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Convert all radians columns to degrees
- Get all columns using << Get Column Names
- Set table to not update using << begin data update
- Loop over them using For Each
- Check if their name has (rad) in it using Contains / Starts With / Ends With
- Use For Each Row to loop over it and change the values to degrees
- 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
- Apply Formula in place (added in JMP18) might work but it is difficult to understand how to use it
- Using matrix values is one good option but For Each Row will work always, always good idea to learn it
- Use substitute and << Set Name to rename the column
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Convert all radians columns to degrees
Always impressive, thank you!
A few follow-on questions:
- Why is "colname" in curly braces as the first argument of the for each loop?
- 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?
- 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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Convert all radians columns to degrees
- It is the syntax for For Each. Conditional and Logical Functions
- 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()
- [] is same as [Row()] and it is used to access current row when using Column(). Iterate on Rows in a Table
-Jarmo