cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
SDF1
Super User

Associative array to rename data table columns

Hi JMP Community,

 

  I have a data table (dt1) with many columns and would like to rename them. They are coded with strange names. But, I have another data table (dt2) with only two columns :Name and :Code. Each row in dt2 has the meaningful name that is associated with the code.

 

  I know how to do this with for loops, but I'm trying to learn something new and hoping to do it through an Associative Array. Something similar to what's in this thread here. I thought I could do something like this:

Names Default To Here( 1 );

dt = Data Table( "dt1" );

dtnames = Data Table( "dt2" );

aa_name_code = Associative Array(
	Column( dtnames, "Name" ) << Get Values,
	Column( dtnames, "Code" ) << Get Values
);

aavalues = aa_name_code << Get Values;
aakeys = aa_name_code << Get Keys;

Eval( Eval Expr( Column(dt,Expr( aavalues )))) << Set Name( Expr( aakeys ) ) ) );

  But, unfortunately, this doesn't work. It's not behaving how I thought the associative arrays behaved.

 

  Again, I want to try and learn something new and get better with a new JSL command, which is why I want to work through the Associative Array. If it's not possible I'll do it the brute-force For-Loop way.

 

Thanks for any thoughts and feedback on this!,

DS

1 ACCEPTED SOLUTION

Accepted Solutions
David_Burnham
Super User (Alumni)

Re: Associative array to rename data table columns

Using associate arrays and for-loops are not mutually exclusive.  I know a lot of people like to avoid for-loops, but at the end of the day you want to iterative over the columns of a table and for-loops are designed to do iteration.

namesDefaultToHere(1);
dt = open("$SAMPLE_DATA/Big Class.jmp");
arr = [=>];
arr["name"] = "first name";
arr["age"] = "age";
arr["sex"] = "gender";
arr["height"] = "height (inches)";
arr["weight"] = "weight (lbs)";
for (i=1,i<=ncols(dt),i++,
	cname = column(i)<<getname;
	column(i)<<setname(arr[cname])
);
-Dave

View solution in original post

4 REPLIES 4
David_Burnham
Super User (Alumni)

Re: Associative array to rename data table columns

Using associate arrays and for-loops are not mutually exclusive.  I know a lot of people like to avoid for-loops, but at the end of the day you want to iterative over the columns of a table and for-loops are designed to do iteration.

namesDefaultToHere(1);
dt = open("$SAMPLE_DATA/Big Class.jmp");
arr = [=>];
arr["name"] = "first name";
arr["age"] = "age";
arr["sex"] = "gender";
arr["height"] = "height (inches)";
arr["weight"] = "weight (lbs)";
for (i=1,i<=ncols(dt),i++,
	cname = column(i)<<getname;
	column(i)<<setname(arr[cname])
);
-Dave
SDF1
Super User

Re: Associative array to rename data table columns

Hi @David_Burnham ,

 

  Thanks for your feedback. I clearly need to do more reading into associative arrays. I don't mind For Loops so much, but was hoping to code the rename in a more elegant way. The link to the other thread was a very elegant solution to re-order column values based on another column's numeric entry. The way the associative array worked in that script was how I thought it would work for the table columns, but it clearly doesn't. I've done column renames with For Loops in the past using lists {...}, but was looking to try a different way without loops. However, I do like the associative array method you presented and will do it that way rather than build lists.

 

  I guess I was thinking that with a list of the column names in the first data table and the associative array from the second data table, one could easily reassign the names because of the associative array, almost like a "recode" function for the column names. 

 

  Anyway, I appreciate your reply and solution to my question of using associative arrays for renaming columns. Time to get back to the data!

 

Thanks!,

DS

David_Burnham
Super User (Alumni)

Re: Associative array to rename data table columns

Just to be clear I hardcoded the array content just so that I had a working example - it still makes sense to populate the keys and values from a separate table.  Notice also that there was no need for any of the Evals, Expr's, etc.  Good luck.

-Dave
SDF1
Super User

Re: Associative array to rename data table columns

Hi @David_Burnham ,

 

  Yes, I followed your example and understood the hardcoding. When working with another table of Name and Code columns, it was easy to create the associative array (note: must be in the right order) so that when setting the column names, it applies the correct value to matching key. I ran it, and it worked perfectly. Again, thanks for the help!

 

DS