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

Changing the name of every third (n+3) column(s) with specific name; 100+ columns

Hi All,

 

I'm trying to name a bunch of columns, 100+ of them. I have been doing a bunch of text-to-columns stuff and have many sets of three columns that I want to give the same names to. See below:

AlphaLion662_3-1678993653136.png

 

Above shows the columns with names that I want to have, to the left of the red line. It also shows the columns with names that I want to replace. As you can see, I'd like to change every third column, starting at n=1 (or whatever column number I want to start at), and every three columns after that (n+3), rename the column to "pX". The same goes for "num_after_pipe" and "value". All of them up to a point. 

 

I'm working with the following script to rename every set of three columns"

 

dt_stack:"Data 1"n<<Set Name("pX");
dt_stack:"Data 2"n<<Set Name("num_after_pipe");
dt_stack:"Data 3"n<<Set Name("value");

dt_stack:"Data 4"n<<Set Name("pX");
dt_stack:"Data 5"n<<Set Name("num_after_pipe");
dt_stack:"Data 6"n<<Set Name("value");

dt_stack:"Data 7"n<<Set Name("pX");
dt_stack:"Data 8"n<<Set Name("num_after_pipe");
.....

 

Any suggestions?

 

Thanks!

 

Regards,

AlphaLion662

1 ACCEPTED SOLUTION

Accepted Solutions
StarfruitBob
Level VI

Re: Changing the name of every third (n+3) column(s) with specific name; 100+ columns

Hello @AlphaLion662,

 

Is this what you were looking for? Keep in mind, I don't know how you want to modify the column names, so I just added prefixes.  You can see the prefixes in the variables first, second, and third.  The column renaming happens in the block under the note "Col name change here".

 

Names default to here(1);
clear log();

dt = open( "$SAMPLE_DATA/Arrhythmia.jmp" );

// Gets all column names in a list
allcols = dt << Get column names();

// Stores the number of columns
numcols = N Items( allcols );

// Some modifier to column names
first = "Tree";
second = "Car";
third = "Star";

For( i = 1, i <= numcols, i++,

	// Temporarly stores the column name for later usage
	temp = column( dt, allcols[i]  ) << Get Name;
	
	if( 
		
		// First col, then every 3rd col thereafter
		i == 1 | Modulo( i - 1, 3 ) == 0,

			// Col name change here
			col_Name1 = column(dt,i) << Get Name;
			newname1 = first || "_" || col_Name1;
			eval( column(allcols[i] ) ) << Set Name( newname1 ),
			
		// Second col, then every 3rd col thereafter
		i == 2 | Modulo( i - 2, 3 ) == 0, //( i - 2 ) % 3

			// Col name change here
			col_Name2 = column(dt,i) << Get Name;
			newname2 = second || "_" || col_Name2;
			eval( column(allcols[i] ) ) << Set Name( newname2 ),
		
		// Third col, then every 3rd col thereafter
		i == 3 | Modulo( i - 3, 3 ) == 0,
			
			// Col name change here
			col_Name3 = column(dt,i) << Get Name;
			newname3 = third || "_" || col_Name3;
			eval( column(allcols[i] ) ) << Set Name( newname3 ),
		
	);	
);
Learning every day!

View solution in original post

3 REPLIES 3
StarfruitBob
Level VI

Re: Changing the name of every third (n+3) column(s) with specific name; 100+ columns

Hello @AlphaLion662,

 

Is this what you were looking for? Keep in mind, I don't know how you want to modify the column names, so I just added prefixes.  You can see the prefixes in the variables first, second, and third.  The column renaming happens in the block under the note "Col name change here".

 

Names default to here(1);
clear log();

dt = open( "$SAMPLE_DATA/Arrhythmia.jmp" );

// Gets all column names in a list
allcols = dt << Get column names();

// Stores the number of columns
numcols = N Items( allcols );

// Some modifier to column names
first = "Tree";
second = "Car";
third = "Star";

For( i = 1, i <= numcols, i++,

	// Temporarly stores the column name for later usage
	temp = column( dt, allcols[i]  ) << Get Name;
	
	if( 
		
		// First col, then every 3rd col thereafter
		i == 1 | Modulo( i - 1, 3 ) == 0,

			// Col name change here
			col_Name1 = column(dt,i) << Get Name;
			newname1 = first || "_" || col_Name1;
			eval( column(allcols[i] ) ) << Set Name( newname1 ),
			
		// Second col, then every 3rd col thereafter
		i == 2 | Modulo( i - 2, 3 ) == 0, //( i - 2 ) % 3

			// Col name change here
			col_Name2 = column(dt,i) << Get Name;
			newname2 = second || "_" || col_Name2;
			eval( column(allcols[i] ) ) << Set Name( newname2 ),
		
		// Third col, then every 3rd col thereafter
		i == 3 | Modulo( i - 3, 3 ) == 0,
			
			// Col name change here
			col_Name3 = column(dt,i) << Get Name;
			newname3 = third || "_" || col_Name3;
			eval( column(allcols[i] ) ) << Set Name( newname3 ),
		
	);	
);
Learning every day!
AlphaLion662
Level II

Re: Changing the name of every third (n+3) column(s) with specific name; 100+ columns

Hey StarfruitBob,

 

After fiddling around with it for a while, this worked for me. Thank you!

jthi
Super User

Re: Changing the name of every third (n+3) column(s) with specific name; 100+ columns

In your case you can also increment loop in 3s and set three columns at the same time in single loop

Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Probe.jmp");
dt << Ungroup Columns(); // easier to see what is going on if we remove Responses column group

start_col_idx = 4;
colnames = {"px", "num_after_pipe", "value"};

For(i = start_col_idx, i <= N Cols(dt), i = i + 3, // we can increment in 3s
	Try( // lazy try-catch to exit when we cannot set more column names
		Column(i) << Set Name(colnames[1]);
		Column(i + 1) << Set Name(colnames[2]);
		Column(i + 2) << Set Name(colnames[3]);
	,
		show(exception_msg);
		break();
	);
);

jthi_0-1679064993074.png

 

-Jarmo