- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
How to rename columns from a list all at once
If I have a data table set up as below
Col1 | Col2 | Col3 | Col4 |
1 | a | e | 20 |
2 | b | f | 21 |
3 | c | g | 22 |
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?
Data1 | Data2 | Data3 | Data4 |
1 | a | e | 20 |
2 | b | f | 21 |
3 | c | g | 22 |
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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!