Hello JMP Community,
I am trying to create a column that indexes rows by an ID column.
In other scripting language, this is accomplished with the following lines.
By ID: gen IDIndex = _n
By ID: gen IDCount = _N
I can do the following, but I index and count the whole table
New Column( "IDIndex",
New Column( "IDCount",
Advice on this would be great!
In your formula you need to check the ID value to see if it has changed from the previous row and reset your index if it has.
Try this formula:
If(:ID != Lag(:ID, 1) | Row() == 1, 1, Lag(:IDIndex, 1) + 1)
Your ID Count column has use IDIndex to get its value:
Col Maximum( :IDIndex, :ID )
The second argument to the Col Maximum function is the By variable.
I hope that helps!
Jeff is correct and i will only add that the formula for IDIndex depends on previous sorting.
If the table needs to be re-sorted later you would need to suppress evaluation or delete it.
column (dt, "IDIndex") << suppress eval ( true );
column (dt, "IDIndex") << delete formula;
to produce the column IDCount without sorting you can use the table summery option:
dt << Summary (Group ( :ID );