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-38311%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EFormula%20for%20Number%20of%20Unique%20Categories%20in%20Column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38311%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EI%20am%20looking%20for%20the%20column%20formula%20to%20provide%20me%20with%20the%20count%20of%20unique%20categories%20in%20a%20column%20(maybe%20based%20on%20another%20columns%20categories).%20For%20instance%2C%20if%20a%20column%20has%3C%2FP%3E%0A%3CP%3EA%3C%2FP%3E%0A%3CP%3EA%3C%2FP%3E%0A%3CP%3EB%3C%2FP%3E%0A%3CP%3EB%3C%2FP%3E%0A%3CP%3EB%3C%2FP%3E%0A%3CP%3EC%3C%2FP%3E%0A%3CP%3Ethen%20I%20would%20like%20to%20get%20back%20the%20value%203.%3C%2FP%3E%0A%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55334%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Formula%20for%20Number%20of%20Unique%20Categories%20in%20Column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55334%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%22By%22%20is%20a%20fairly%20global%20concept%20in%20JMP.%26nbsp%3B%20If%20you%20run%20the%20Distribution%20Platform%2C%20and%20specify%20a%20%22By%22%20variable%20it%20will%20generate%20a%20separate%20Distribution%20for%20each%20of%20the%20different%20values%20of%20the%20%22By%22%20columns.%26nbsp%3B%20The%20same%20is%20for%20the%20Summarize()%20function.%26nbsp%3B%20Here%20is%20a%20quote%20from%20the%20JSL%20Syntax%20Reference%20for%20the%20Summarize()%20function.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BHelp%3D%3D%26gt%3BBooks%3D%3D%26gt%3BJSL%20Syntax%20Reference%3C%2FP%3E%0A%3CP%3E%22Using%20a%20BY%20variable%20changes%20the%20output%20from%20single%20values%20for%20each%20statistic%20to%20a%20list%20of%20values%20for%20each%20group%20in%20the%20BY%20variable.%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EGood%20documentation%20along%20with%20an%20example%20can%20also%20be%20fount%20in%20the%20Scripting%20Index%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BHelp%3D%3D%26gt%3BScripting%20Index%3D%3D%26gt%3BFunctions%3D%3D%26gt%3BStatistical%3D%3D%26gt%3BSumarize%3C%2FP%3E%0A%3CP%3Eor%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BHelp%3D%3D%26gt%3BScripting%20Index.........search%20on%20Summarize%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55330%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Formula%20for%20Number%20of%20Unique%20Categories%20in%20Column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55330%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EMr.%20Nelson%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20describe%20in%20detail%20what%20this%20function%20is%20doing%3F%20Not%20sure%20I%20understand%20the%20%22by%22%20function.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EMartin%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55305%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Formula%20for%20Number%20of%20Unique%20Categories%20in%20Column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55305%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThat%20worked%20great!%20I%20was%20thrown%20off%20by%20the%20red%20underline%2C%20I%20thought%20that%20meant%20it%20wasn't%20going%20to%20work.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20466px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20466px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20466px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F10453i279D01A72CFCF8B1%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20not%20actually%20sure%20I've%20ever%20tried%20pasting%20a%20formula%20directly%20in%20the%20column%20before%2C%20I%20thought%20I%20had%20to%20rebuild%20it%20using%20the%20explicit%20formula%20column%20functions...%20this%20is%20a%20huge%20improvement.%20Thank%20you%20so%20much%20for%20the%20quick%20reply!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAron%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55292%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Formula%20for%20Number%20of%20Unique%20Categories%20in%20Column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55292%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EWhat%20I%20always%20forget%2C%20is%20that%20a%20column%20formula%20can%20actually%20be%20an%20entire%20mini%20script%3B%20with%20many%20semicolons%20and%20lines%20of%20code.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55284%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Formula%20for%20Number%20of%20Unique%20Categories%20in%20Column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55284%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThe%20stated%20formula%20from%20my%20last%20response%20should%20do%20what%20you%20want.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EIf(%20Row()%20%3D%3D%201%2C%0A%20Summarize(%20x%20%3D%20by(%20%3ACustomer_ID)%20)%3B%0A%20y%20%3D%20N%20Items(%20x%20)%3B%0A)%3B%0Ay%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EJust%20cut%20and%20paste%20this%20into%20the%20formula%20editor%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55272%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Formula%20for%20Number%20of%20Unique%20Categories%20in%20Column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55272%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EI%20see%20how%20this%20would%20work%20for%20a%20script%20to%20create%20a%20column%2C%20but%20I've%20created%20a%20datatable%20that%20I%20update%20from%20a%20database%20and%20I'd%20like%20to%20have%20a%20column%20in%20that%20table%20with%20a%20formula%20to%20calculate%20the%20number%20of%20unique%20categories.%20Is%20there%20a%20way%20to%20do%20that%20directly%20in%20a%20column%20formula%20instead%20of%20though%20a%20script%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20found%20a%20hack%20using%26nbsp%3Busing%20Def()%20and%20Col%20Sum%20but%20it%20only%20works%20because%20i'm%20looking%20at%20a%20sorted%20field%20that's%20a%20unique%20id%20with%20numbrers.%20It%20doesn't%20work%20for%20actual%20text%20variables.%20Is%20there%20a%20better%20solution%20for%20that%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20the%20hacked%20formula%3A%3C%2FP%3E%3CP%3ECol%20Sum(%20Dif(%20%3Acustomer_id%20)%20%26gt%3B%200%20)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%2C%3C%2FP%3E%3CP%3EAron%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38489%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Formula%20for%20Number%20of%20Unique%20Categories%20in%20Column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38489%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EThis%20is%20PERFECT!%20Thank%20you%20Jim!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38313%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Formula%20for%20Number%20of%20Unique%20Categories%20in%20Column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38313%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ETry%20this%20formula.......of%20course%2C%20swap%20out%20the%20%3AFlow%20for%20the%20column%20you%20want%20to%20count%20the%20categories%20for%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ESummarize(%20x%20%3D%20by(%20%3AFLOW%20)%20)%3B%0AN%20Items(%20x%20)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EIf%20the%20data%20table%20is%20really%20large%2C%20you%20may%20want%20to%20use%20this%20more%20efficient%20version%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EIf(%20Row()%20%3D%3D%201%2C%0A%20Summarize(%20x%20%3D%20by(%20%3AFlow%20)%20)%3B%0A%20y%20%3D%20N%20Items(%20x%20)%3B%0A)%3B%0Ay%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-38312%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Formula%20for%20Number%20of%20Unique%20Categories%20in%20Column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-38312%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EI%20think%20I%20remember%20the%20Summation%20formula%20being%20used%20for%20this%2C%20but%20my%20memory%20fails%20me.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
Martin
Level V

Formula for Number of Unique Categories in Column

I am looking for the column formula to provide me with the count of unique categories in a column (maybe based on another columns categories). For instance, if a column has

A

A

B

B

B

C

then I would like to get back the value 3.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Formula for Number of Unique Categories in Column

Try this formula.......of course, swap out the :Flow for the column you want to count the categories for

Summarize( x = by( :FLOW ) );
N Items( x );

If the data table is really large, you may want to use this more efficient version

If( Row() == 1,
	Summarize( x = by( :Flow ) );
	y = N Items( x );
);
y;

 

Jim

View solution in original post

9 REPLIES 9
Martin
Level V

Re: Formula for Number of Unique Categories in Column

I think I remember the Summation formula being used for this, but my memory fails me.

txnelson
Super User

Re: Formula for Number of Unique Categories in Column

Try this formula.......of course, swap out the :Flow for the column you want to count the categories for

Summarize( x = by( :FLOW ) );
N Items( x );

If the data table is really large, you may want to use this more efficient version

If( Row() == 1,
	Summarize( x = by( :Flow ) );
	y = N Items( x );
);
y;

 

Jim
Martin
Level V

Re: Formula for Number of Unique Categories in Column

This is PERFECT! Thank you Jim!
aronwilson
Level II

Re: Formula for Number of Unique Categories in Column

I see how this would work for a script to create a column, but I've created a datatable that I update from a database and I'd like to have a column in that table with a formula to calculate the number of unique categories. Is there a way to do that directly in a column formula instead of though a script?

 

I found a hack using using Def() and Col Sum but it only works because i'm looking at a sorted field that's a unique id with numbrers. It doesn't work for actual text variables. Is there a better solution for that?

 

Here is the hacked formula:

Col Sum( Dif( :customer_id ) > 0 )

 

Thank you,

Aron

txnelson
Super User

Re: Formula for Number of Unique Categories in Column

The stated formula from my last response should do what you want.  

If( Row() == 1,
	Summarize( x = by( :Customer_ID) );
	y = N Items( x );
);
y;

Just cut and paste this into the formula editor

Jim
Martin
Level V

Re: Formula for Number of Unique Categories in Column

What I always forget, is that a column formula can actually be an entire mini script; with many semicolons and lines of code.

aronwilson
Level II

Re: Formula for Number of Unique Categories in Column

That worked great! I was thrown off by the red underline, I thought that meant it wasn't going to work.

image.png

 

I'm not actually sure I've ever tried pasting a formula directly in the column before, I thought I had to rebuild it using the explicit formula column functions... this is a huge improvement. Thank you so much for the quick reply!

 

Aron

Martin
Level V

Re: Formula for Number of Unique Categories in Column

Mr. Nelson,

 

Can you describe in detail what this function is doing? Not sure I understand the "by" function.

 

Thanks,

Martin

txnelson
Super User

Re: Formula for Number of Unique Categories in Column

"By" is a fairly global concept in JMP.  If you run the Distribution Platform, and specify a "By" variable it will generate a separate Distribution for each of the different values of the "By" columns.  The same is for the Summarize() function.  Here is a quote from the JSL Syntax Reference for the Summarize() function.

     Help==>Books==>JSL Syntax Reference

"Using a BY variable changes the output from single values for each statistic to a list of values for each group in the BY variable."

 

Good documentation along with an example can also be fount in the Scripting Index

     Help==>Scripting Index==>Functions==>Statistical==>Sumarize

or

     Help==>Scripting Index.........search on Summarize

Jim

Recommended Articles