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
user8421
Level II

How to rename columns from a list all at once

If I have a data table set up as below

Col1Col2Col3Col4
1ae20
2bf21
3cg22


and a list of strings for new column names
NewCols = { "Data1", "Data2", "Data3", "Data4"}

How can I write a script to rename the columns of the data table all once using the list New Cols such that it results in the following data table?

Data1Data2Data3Data4
1ae20
2bf21
3cg22



1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How to rename columns from a list all at once

I am not aware of a single statement that will change the column names.  Below is the method that I use in such situations.

Names Default To Here( 1 );
dt = New Table( "Example",
	Add Rows( 3 ),
	New Column( "Col1",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Selected,
		Set Values( [1, 2, 3] )
	),
	New Column( "Col2", Character( 16 ), "Nominal", Set Values( {"a", "b", "c"} ) ),
	New Column( "Col3", Character( 16 ), "Nominal", Set Values( {"e", "f", "g"} ) ),
	New Column( "Col4",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [20, 21, 22] )
	)
);

NewCols = {"Data1", "Data2", "Data3", "Data4"};

For( i = 1, i <= N Items( NewCols ), i++,
	Column( dt, i ) << set name( NewCols[i] )
);
Jim

View solution in original post

3 REPLIES 3
txnelson
Super User

Re: How to rename columns from a list all at once

I am not aware of a single statement that will change the column names.  Below is the method that I use in such situations.

Names Default To Here( 1 );
dt = New Table( "Example",
	Add Rows( 3 ),
	New Column( "Col1",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Selected,
		Set Values( [1, 2, 3] )
	),
	New Column( "Col2", Character( 16 ), "Nominal", Set Values( {"a", "b", "c"} ) ),
	New Column( "Col3", Character( 16 ), "Nominal", Set Values( {"e", "f", "g"} ) ),
	New Column( "Col4",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [20, 21, 22] )
	)
);

NewCols = {"Data1", "Data2", "Data3", "Data4"};

For( i = 1, i <= N Items( NewCols ), i++,
	Column( dt, i ) << set name( NewCols[i] )
);
Jim
Thierry_S
Super User

Re: How to rename columns from a list all at once

Hi,
To add to @txnelson scripting option, there is a copy / paste option that not all JMP users are familiar with, which works well if you have an existing list for the new colunm names:
1) Copy the list of new column names from another table, a set of rows in your table, or from an external file (e.g. Excel).
2) In the column list (for left list box in your table view), select the columns to be renamed and paste
3) Importantly, the order of the new column names has to match the order of the destination columns.
It is not as elegant as a scripting solution but it is rather useful to get this renaming task done quickly.
Best,
TS
Thierry R. Sornasse
julian
Community Manager Community Manager

Re: How to rename columns from a list all at once

Hi @user8421,

It may not be applicable to you yet, but in JMP 16 (and later) you can use an improved For Each() jsl command to do this quickly: 

 

dt = Current Data Table();
NewCols = { "Data1", "Data2", "Data3", "Data4"}
For Each( {value, index}, NewCols, Column( dt, index ) << set name( value ) );

I hope this helps!

@julian