cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
%3CLINGO-SUB%20id%3D%22lingo-sub-347226%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E4%BD%BF%E7%94%A8%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E9%87%8D%E5%91%BD%E5%90%8D%E5%88%97%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-347226%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%E5%97%A8%EF%BC%8C%E6%88%91%E6%AD%A3%E5%9C%A8%E5%98%97%E8%A9%A6%E4%BD%BF%E7%94%A8%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E9%87%8D%E5%91%BD%E5%90%8D%E4%B8%80%E5%A0%86%E5%88%97%E3%80%82%20%E6%88%91%E4%B8%80%E7%9B%B4%E4%BB%A5%E5%A4%A7%E9%A1%9E%E6%95%B8%E6%93%9A%E7%82%BA%E4%BE%8B%E3%80%82%20%E6%88%91%E5%B0%87%E4%B8%8D%E5%8B%9D%E6%84%9F%E6%BF%80%E4%B8%80%E4%BA%9B%E4%BF%AE%E5%BE%A9%E6%88%91%E7%9A%84%E8%85%B3%E6%9C%AC%E7%9A%84%E5%B9%AB%E5%8A%A9%E3%80%82%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(%201%20)%3B%0Adt%20%3D%20Data%20Table%20(%22Big%20Class.jmp%22)%3B%0A%2F%2FUse%20and%20array%20to%20rename%20the%20Big%20Class%20table%20columns%20to%20%22spanish%22%0AnewColNames%20%3D%20Associative%20Array()%3B%0AnewColNames%5B%22name%22%5D%20%3D%20%22nombre%22%3B%0AnewColNames%5B%22age%22%5D%20%3D%20%22edad%22%3B%0AnewColNames%5B%22sex%22%5D%20%3D%20%22sexo%22%3B%0AnewColNames%5B%22height%22%5D%20%3D%20%22altura%22%3B%0AnewColNames%5B%22weight%22%5D%20%3D%20%22peso%22%3B%0A%0Auno%20%3D%20newColNames%20%26lt%3B%26lt%3B%20Get%20Keys()%3B%20%2F%2Ffor%20testing%0A%0AnewN%20%3D%20newColNames%20%26lt%3B%26lt%3B%20Get%20Values(%7B%22name%22%7D)%3B%20%2F%2Ffor%20testing%0A%0AoriNames%20%3D%20dt%20%26lt%3B%26lt%3B%20Get%20Column%20Names()%3B%20%2F%2F%20in%20case%20there%20are%20variation%20on%20the%20number%2Fname%20of%20columns%0A%0AFor(%20i%3D1%2C%201%20%26lt%3B%3D%20N%20Items(oriNames)%2C%20i%2B%2B%2C%0Akey%20%3D%20oriNames%5Bi%5D%3B%0A%0A%20Column(dt%2C%20key%20)%20%26lt%3B%26lt%3B%20set%20name%20(newColNames%26lt%3B%3CGET%20values%3D%22%22%3E%3C%2FGET%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-347226%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CLINGO-LABEL%3E%E6%95%B8%E6%93%9A%E8%A1%A8%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3E%E8%85%B3%E6%9C%AC%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-347350%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E4%BD%BF%E7%94%A8%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E9%87%8D%E5%91%BD%E5%90%8D%E5%88%97%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-347350%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F3605%22%20target%3D%22_blank%22%3E%40ian_jmp%3C%2FA%3E%3C%2FP%3E%3CP%3E%E9%9D%9E%E5%B8%B8%E6%84%9F%E8%AC%9D%E4%BD%A0%E7%9A%84%E5%B9%AB%E5%8A%A9%E3%80%82%20%E9%87%8D%E8%A6%81%E8%AA%B2%E7%A8%8B%EF%BC%9A%E6%AA%A2%E6%9F%A5%E4%BD%A0%E7%9A%84%20i's%20%E5%92%8C%E2%80%9C%E7%8D%B2%E5%8F%96%E5%80%BC%E2%80%9D%E5%92%8C%E2%80%9C%E7%8D%B2%E5%8F%96%E5%80%BC%E2%80%9D%E3%80%82%3C%2FP%3E%3CP%3E%E6%AD%A4%E8%87%B4%EF%BC%8C%3C%2FP%3E%3CP%3E%E9%98%BF%E7%88%BE%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-347328%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E4%BD%BF%E7%94%A8%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E9%87%8D%E5%91%BD%E5%90%8D%E5%88%97%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-347328%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F3605%22%20target%3D%22_blank%22%3E%40ian_jmp%3C%2FA%3E%E6%88%91%E8%8A%B1%E4%BA%86%E5%A4%AA%E9%95%B7%E6%99%82%E9%96%93%E6%89%8D%E7%9C%8B%E5%88%B0%E2%80%9C1%E2%80%9D%EF%BC%81%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Edt%20%3D%20Open(%20%22%24sample_data%2FBig%20Class.jmp%22%20)%3B%0A%2F%2FUse%20and%20array%20to%20rename%20the%20Big%20Class%20table%20columns%20to%20%22spanish%22%0AnewColNames%20%3D%20Associative%20Array()%3B%0AnewColNames%5B%22name%22%5D%20%3D%20%22nombre%22%3B%0AnewColNames%5B%22age%22%5D%20%3D%20%22edad%22%3B%0AnewColNames%5B%22sex%22%5D%20%3D%20%22sexo%22%3B%0AnewColNames%5B%22height%22%5D%20%3D%20%22altura%22%3B%0AnewColNames%5B%22weight%22%5D%20%3D%20%22peso%22%3B%0A%0Auno%20%3D%20newColNames%20%26lt%3B%26lt%3B%20Get%20Keys()%3B%20%2F%2Ffor%20testing%0A%0AnewN%20%3D%20newColNames%20%26lt%3B%26lt%3B%20Get%20Values(%20%7B%22age%22%7D%20)%3B%20%2F%2Ffor%20testing%0A%0AoriNames%20%3D%20dt%20%26lt%3B%26lt%3B%20Get%20Column%20Names(%20%22string%22%20)%3B%20%2F%2F%20in%20case%20there%20are%20variation%20on%20the%20number%2Fname%20of%20columns%0A%0AFor(%20i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Items(%20oriNames%20)%2C%20i%2B%2B%2C%0A%20key%20%3D%20oriNames%5Bi%5D%3B%0A%20Column(%20dt%2C%20key%20)%20%26lt%3B%26lt%3B%20set%20name(%20newColNames%5Bkey%5D%20)%3B%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-347327%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E4%BD%BF%E7%94%A8%E9%97%9C%E8%81%AF%E6%95%B8%E7%B5%84%E9%87%8D%E5%91%BD%E5%90%8D%E5%88%97%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-347327%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%E4%BD%A0%E9%9D%9E%E5%B8%B8%E6%8E%A5%E8%BF%91%EF%BC%9A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(%201%20)%3B%0A%0Adt%20%3D%20Open(%22%24SAMPLE_DATA%2FBig%20Class.jmp%22)%3B%0A%0A%2F%2FUse%20and%20array%20to%20rename%20the%20Big%20Class%20table%20columns%20to%20%22spanish%22%0AnewColNames%20%3D%20Associative%20Array()%3B%0AnewColNames%5B%22name%22%5D%20%3D%20%22nombre%22%3B%0AnewColNames%5B%22age%22%5D%20%3D%20%22edad%22%3B%0AnewColNames%5B%22sex%22%5D%20%3D%20%22sexo%22%3B%0AnewColNames%5B%22height%22%5D%20%3D%20%22altura%22%3B%0AnewColNames%5B%22weight%22%5D%20%3D%20%22peso%22%3B%0A%0AoriNames%20%3D%20dt%20%26lt%3B%26lt%3B%20Get%20Column%20Names(%22String%22)%3B%20%20%2F%2F%20in%20case%20there%20are%20variation%20on%20the%20number%2Fname%20of%20columns%0A%0AFor(%20i%3D1%2C%20i%20%26lt%3B%3D%20N%20Items(oriNames)%2C%20i%2B%2B%2C%0A%20key%20%3D%20oriNames%5Bi%5D%3B%0A%20Column(dt%2C%20key%20)%20%26lt%3B%26lt%3B%20set%20name%20(%20newColNames%20%26lt%3B%26lt%3B%20Get%20Value%20(key)%20)%3B%0A)%3B%20%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
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