cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
achid03
Level I

Find string in column, rename the column and add new column in JSL

Hi All,

 

I have a table like below and want to find the String "Z", "A" and "B" in the columns and rename it to "Z_A" and "Z_B".

After that, I want to add a column after X_loc and Y_loc which converts them to mm. The formula that I want to use is (X_Loc / 1000).

Can anyone guide me on how to do the scripting for this?

Thanks

 

achid03_0-1625641187776.png

 

1 REPLY 1
txnelson
Super User

Re: Find string in column, rename the column and add new column in JSL

  1. You need to read the Discovering JMP document, and the Scripting Guide!
  2. You need to attempt to solve the problem first.  One learns by making mistakes and figuring out what the problem is
  3. When you fail at solving the problem, the Discussion Community is there to help.
  4. Below is a script that if you have setup your data table correctly, should show you one way to solve the problem
    Names Default To Here( 1 );
    
    // Create a sample table
    dt = New Table( "Example",
    	Add Rows( 3 ),
    	New Column( "ID", Character, "Nominal", Set Values( {"#1", "#2", "#3"} ) ),
    	New Column( "X_loc", Set Values( [2, 3, 4] ) ),
    	New Column( "Y_loc", Set Values( [2, 2, 4] ) ),
    	New Column( "Z_Surce_Column_A", Set Values( [4, 5, -4] ), Set Display Width( 118 ) ),
    	New Column( "Z_Source_Column_B", Set Values( [-4, -10, 1] ), Set Display Width( 133 ) )
    );
    
    // Wait 5 seconds so you can see the beginning table
    wait(5);
    
    // Loop across the columns to find the ones to change
    For( i = 1, i <= N Cols( dt ), i++,
    	If(
    		Contains( Column( dt, i ) << get name, "Z" ) & Contains( Column( dt, i ) << get name, "A" ),
    			Column( dt, i ) << set name( "Z_A" ),
    		Contains( Column( dt, i ) << get name, "Z" ) & Contains( Column( dt, i ) << get name, "B" ),
    			Column( dt, i ) << set name( "Z_B" )
    	)
    );
    
    // Create the new column and move it to where it is to be located
    dt << New Column("The New Column", formula(:X_loc/1000));
    dt << go to(:The New Column );
    dt << Move Selected Columns(After(:Y_loc));

    You need to study the script and learn how it works.

Jim