cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Learn how to build custom Python data connectors and further customize JMP’s Data Connector Framework with the Python Data Connector Demo, available now in the JMP Marketplace!
  • See how to create experiments to support product design and ID useful product features. Register for June 12 webinar, 2pm US Eastern Time.

Discussions

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

Increment a counter from 1 to n for every unique set of identical rows

Hi everyone,

 

I figure what I want to do is really easy, I just haven't come up with a good way to do it. I have a large table with a substantial number of identical rows which I want to rename uniquely but in a reproducible way. Currently, I have a script that takes the following column:

 

A number 1

B number 1

A number 2

A number 3

A number 3

A number 3

B number 3

B number 3

B number 3

...

 

Into into a new names column, using the row index identifier:

 

A number 1

B number 1

A number 2

A number 3

A number 3 5

A number 3 6

B number 3

B number 3 8

B number 3 9

 

 

That code is

 

Names Default To Here( 0 );
RawFile << New Column( "itemname2", Character, Nominal );
Current Data Table( RawFile );
For( i = 2, i <= N Rows( RawFile ), i++,
	If( :itemname[i] == :itemname[i - 1],
		:itemname2[i] = :itemname[i] || " " || Char( i ),
		:itemname2[i] = :itemname[i];
	);
);

 

 

This works well enough, but the issue here is I want the output to instead increment from 1 each time it needs to be used, to give something like

 

A number 1

B number 1

A number 2

A number 3

A number 3 1

A number 3 2

B number 3

B number 3 1

B number 3 2

 

I know this should be simple, but I'm not sure how to structure/limit the loop....any help would be appreciated. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
screech
Level I

Re: Increment a counter from 1 to n for every unique set of identical rows

Nevermind.....realized I just needed the initialization of the second counter outside the loop. Cheers!

 

Names Default To Here( 0 );
RawFile << New Column( "itemname2", Character, Nominal );
Current Data Table( RawFile );
k=1; For( i = 2, i <= N Rows( RawFile ), i++, If( :itemname[i] == :itemname[i - 1], :itemname2[i] = :itemname[i] || " " || Char( k );
k++, :itemname2[i] = :itemname[i];
k=1 ); );

View solution in original post

1 REPLY 1
screech
Level I

Re: Increment a counter from 1 to n for every unique set of identical rows

Nevermind.....realized I just needed the initialization of the second counter outside the loop. Cheers!

 

Names Default To Here( 0 );
RawFile << New Column( "itemname2", Character, Nominal );
Current Data Table( RawFile );
k=1; For( i = 2, i <= N Rows( RawFile ), i++, If( :itemname[i] == :itemname[i - 1], :itemname2[i] = :itemname[i] || " " || Char( k );
k++, :itemname2[i] = :itemname[i];
k=1 ); );

Recommended Articles