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
hogi
Level XII

Append a string to column names

What is the easiest way to append a string to columns names?

Not to all of them, but just to some selected ones.

 

Up to now, I used Rename Columns from the Jmp Scripting tools Add-In :
Scripting Tools 

hogi_0-1679406742553.png

But the actions of the Add-In are not captured by the Workflow Builder.

This is why I am interested in how to do it directly in Jmp without the help of an add-in.

 

Replace String from the Recode Column Names menu looks similar.

a)  Is there an option to apply the change just to some selected columns?

b) How must the Regex expression look like to add a string to the existing column names?
   Using the same syntax as in Jmp Scripting tools/Rename Columns,  the appended string is added twice:

hogi_1-1679407747917.png

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Append a string to column names

Here is an example of the way I approach adding text to a column name.

Names Default To Here( 1 );
dt = 
// Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

dt << select columns( :height, :weight );

selCols = dt << get selected columns( string );

For Each( {col}, selCols,
	Column( col ) << set name( col || " " || "Added Text" )
);
Jim

View solution in original post

6 REPLIES 6
txnelson
Super User

Re: Append a string to column names

Here is an example of the way I approach adding text to a column name.

Names Default To Here( 1 );
dt = 
// Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

dt << select columns( :height, :weight );

selCols = dt << get selected columns( string );

For Each( {col}, selCols,
	Column( col ) << set name( col || " " || "Added Text" )
);
Jim

Re: Append a string to column names

This might be a completely different direction than you were thinking, but the attached workflow adds a string to all the column names by moving the column names down, transposing the table, adding a column with the constant string of 'abcd,' then concatenating this new column with the column names, and finishes by re-transposing and moving the column names up. It does change all the column modeling data types though, which is likely a problem.

 

To make it add to just some of the column names, you could use a formula column, rather than a constant column.

 

Again, this is less of a solution and more of a potential catalyst for a way to think about it. Perhaps others will chime in with better ideas.

 

jthi
Super User

Re: Append a string to column names

Or using a bit similar idea as @Jed_Campbell had, you could

  1. select the columns of interest and first row
  2. subset with selected rows and columns
  3. drop headers down
  4. transpose
  5. create your new column names
  6. go to recode
  7. use apply mapping from table

 

jthi_1-1679420193421.png

jthi_2-1679420204899.png

jthi_3-1679420216070.png

jthi_0-1679420160617.png

 

-Jarmo
jthi
Super User

Re: Append a string to column names

For regex try using + instead of *

jthi_0-1679413792661.png

 

-Jarmo
hogi
Level XII

Re: Append a string to column names

great, thanks @jthi .


Hm, for the "apply to some columns" I hope there is some easy solution hiding somewhere in Recode?

hogi
Level XII

Re: Append a string to column names

Ok, doesn't seem so ....
So, Jmp could be improved by copying this functionality from the Scripting Tools .

Here you can find the corresponding suggestion in the Wish List:
Recode and Recode Column Names: apply for specific rows/columns