cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • New to JMP? Join us Sept. 23-24 for the Early User Edition of Discovery Summit, tailor-made for new users. Register now for free!
  • Your voice matters! Tell us how you prefer to receive JMP updates, so we can tailor our communication to your needs. Take short survey.
  • See how to access JMP Marketplace - and - find, create & share add-ins to extend your JMP. Watch video.

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
ALopez
Level III

Rename columns using an associative array

Hi, I am trying to use an associative array to rename a bunch of columns. I have been using the Big Class data as an example. I will appreciate some help fixing my script.

Names Default To Here( 1 );
dt = Data Table ("Big Class.jmp");
//Use and array to rename the Big Class table columns to "spanish"
newColNames = Associative Array();
newColNames["name"] = "nombre";
newColNames["age"] = "edad";
newColNames["sex"] = "sexo";
newColNames["height"] = "altura";
newColNames["weight"] = "peso";

uno = newColNames << Get Keys(); //for testing

newN = newColNames << Get Values({"name"}); //for testing

oriNames = dt << Get Column Names(); // in case there are variation on the number/name of columns

For( i=1, 1 <= N Items(oriNames), i++,
key = oriNames[i];

	Column(dt, key ) << set name (newColNames<<Get Values ({key}));
	
); 
1 ACCEPTED SOLUTION

Accepted Solutions
ian_jmp
Level X

Re: Rename columns using an associative array

You were very close:

Names Default To Here( 1 );

dt = Open("$SAMPLE_DATA/Big Class.jmp");

//Use and array to rename the Big Class table columns to "spanish"
newColNames = Associative Array();
newColNames["name"] = "nombre";
newColNames["age"] = "edad";
newColNames["sex"] = "sexo";
newColNames["height"] = "altura";
newColNames["weight"] = "peso";

oriNames = dt << Get Column Names("String"); 	// in case there are variation on the number/name of columns

For( i=1, i <= N Items(oriNames), i++,
	key = oriNames[i];
	Column(dt, key ) << set name ( newColNames << Get Value (key) );
); 

View solution in original post

3 REPLIES 3
ian_jmp
Level X

Re: Rename columns using an associative array

You were very close:

Names Default To Here( 1 );

dt = Open("$SAMPLE_DATA/Big Class.jmp");

//Use and array to rename the Big Class table columns to "spanish"
newColNames = Associative Array();
newColNames["name"] = "nombre";
newColNames["age"] = "edad";
newColNames["sex"] = "sexo";
newColNames["height"] = "altura";
newColNames["weight"] = "peso";

oriNames = dt << Get Column Names("String"); 	// in case there are variation on the number/name of columns

For( i=1, i <= N Items(oriNames), i++,
	key = oriNames[i];
	Column(dt, key ) << set name ( newColNames << Get Value (key) );
); 
Craige_Hales
Super User

Re: Rename columns using an associative array

@ian_jmp Took me way too long to see the "1"!

dt = Open( "$sample_data/Big Class.jmp" );
//Use and array to rename the Big Class table columns to "spanish"
newColNames = Associative Array();
newColNames["name"] = "nombre";
newColNames["age"] = "edad";
newColNames["sex"] = "sexo";
newColNames["height"] = "altura";
newColNames["weight"] = "peso";

uno = newColNames << Get Keys(); //for testing

newN = newColNames << Get Values( {"age"} ); //for testing

oriNames = dt << Get Column Names( "string" ); // in case there are variation on the number/name of columns

For( i = 1, i <= N Items( oriNames ), i++,
	key = oriNames[i];
	Column( dt, key ) << set name( newColNames[key] );
);
Craige
ALopez
Level III

Re: Rename columns using an associative array

@ian_jmp 

Thank you very much for you help. Important lessons: check you i's and "Get Values" and "Get Value".

Best regards,

AL 

Recommended Articles