cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-51256%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EGet%20a%20list%20of%20the%20unique%20values%20in%20a%20column%2C%20matrix%2C%20or%20list%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-51256%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CDIV%20class%3D%22lia-message-template-problem-zone%22%3EProblem%3CP%3EProblem%201%3A%26nbsp%3B%20I%20need%20to%20get%20a%20list%20of%20the%20unique%20values%20in%20a%20data%20table%20column.%3C%2FP%3E%3CP%3EProblem%202%3A%26nbsp%3B%20I%20need%20to%20get%20a%20list%20of%20the%20unique%20values%20in%20a%20JSL%20data%20structure%20(list%20or%20matrix)%3C%2FP%3E%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3ESolution%3CP%3E%3CU%3EFor%20problem%201%3A%3C%2FU%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ESolution%201%3A%3C%2FSTRONG%3E%26nbsp%3B%20Use%20summary%20tables.%26nbsp%3B%20The%20JSL%20below%20shows%20how%20to%20use%20Summary()%20to%20get%20a%20table%20that%20will%20have%20a%20row%20with%20each%20unique%20value%20in%20a%20single%20column.%26nbsp%3B%20%26nbsp%3BYou%20can%20then%20do%20further%20JSL%20table%20commands%20to%20get%20those%20values%20from%20the%20summary%20table.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E_dt%20%3D%20Open(%20%22%24sample_data%2Fbig%20class.jmp%22%20)%3B%0A%2F%2Fcreate%20a%20summary%20table%20of%20the%20%3Asex%20column%0A_dt1%20%3D%20_dt%20%26lt%3B%26lt%3B%20Summary(%0A%20Group(%20%3Asex%20)%2C%0A%20Freq(%20%22None%22%20)%2C%0A%20Weight(%20%22None%22%20)%0A)%3B%0A%2F%2Fcreate%20a%20summary%20table%20of%20the%20%3Aage%20column%0A_dt2%20%3D%20_dt%20%26lt%3B%26lt%3B%20Summary(%0A%20Group(%20%3Aage%20)%2C%0A%20Freq(%20%22None%22%20)%2C%0A%20Weight(%20%22None%22%20)%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222-13-2018%205-22-08%20PM.png%22%20style%3D%22width%3A%20280px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%222-13-2018%205-22-08%20PM.png%22%20style%3D%22width%3A%20280px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%222-13-2018%205-22-08%20PM.png%22%20style%3D%22width%3A%20280px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F9328i5A075F342798D202%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%222-13-2018%205-22-08%20PM.png%22%20alt%3D%222-13-2018%205-22-08%20PM.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FCODE%3E%3C%2FP%3E%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%3CU%3EProblem%202%3A%26nbsp%3B%26nbsp%3B%3C%2FU%3E%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3ENow%2C%26nbsp%3B%20instead%20of%20a%20data%20table%20you%20have%20a%20JMP%20list%20or%20matrix%3A%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Esexlist%20%3D%20%7B%22F%22%2C%20%22M%22%2C%20%22M%22%2C%20%22F%22%2C%20%22F%22%2C%20%22M%22%2C%20%22M%22%2C%20%22F%22%2C%20%22M%22%7D%3B%0Aagelist%20%3D%20%5B14%2C%2011%2C%2012%2C%2013%2C%2015%2C%2017%2C%2015%2C%2011%2C%2012%5D%3B%3C%2FCODE%3E%3C%2FPRE%3Ethe%20complicated%20ways%20to%20do%20this%20would%20be%3A%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3Ea)%20put%20the%20values%20in%20the%20list%20into%20a%20new%20data%20table%20column%2C%20and%20then%20use%20the%20summary%20approach%2C%20OR%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3Eb)%20write%20a%20somewhat%20complicated%20for%20loop%20to%20search%20for%20and%20record%20unique%20values%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3EFortunately%2C%20there%20is%20an%20easier%20way%2C%20using%20associative%20arrays.%26nbsp%3B%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EAssociative%20Array(sexlist)%20%26lt%3B%26lt%3B%20Get%20Keys%3B%0AAssociative%20Array(agelist)%20%26lt%3B%26lt%3B%20Get%20Keys%3B%3C%2FCODE%3E%3C%2FPRE%3Ereturns%26nbsp%3B%7B%22F%22%2C%20%22M%22%7D%20and%26nbsp%3B%7B11%2C%2012%2C%2013%2C%2014%2C%2015%2C%2017%7D%2C%20respectively%20(note%20that%20these%20are%20nice%20sorted%20list!).%26nbsp%3B%20If%20you%20would%20like%20the%20numeric%20values%20as%20a%20sorted%20vector%2C%20you%20can%20use%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EMatrix(Associative%20Array(agelist)%20%26lt%3B%26lt%3B%20Get%20Keys)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3Ewhich%20returns%26nbsp%3B%5B11%2C%2012%2C%2013%2C%2014%2C%2015%2C%2017%5D.%26nbsp%3B%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3EThis%20approach%20can%20also%20be%20used%20in%20place%20of%20Solution%201%20for%20Problem%201%3A%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%3CU%3EProblem1%2C%20Solution%202%3A%3C%2FU%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EAssociative%20Array(Column(_dt%2C%20%22sex%22)%20%26lt%3B%26lt%3B%20Get%20Values)%20%26lt%3B%26lt%3B%20Get%20Keys%3B%3CBR%20%2F%3EMatrix(Associative%20Array(Column(_dt%2C%20%22age%22)%26lt%3B%26lt%3B%20Get%20Values)%26lt%3B%3CGET%20keys%3D%22%22%3E%3C%2FGET%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3Ereturns%26nbsp%3B%3CSPAN%3E%7B%22F%22%2C%20%22M%22%7D%20and%20%5B11%2C%2012%2C%2013%2C%2014%2C%2015%2C%2017%5D.%2C%20respectively.%20What%20this%20is%20doing%20is%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CUL%3E%3CLI%3E%3CSPAN%3Eget%20the%20values%20from%20the%20specified%20column%20as%20a%20list%20(if%20the%20column%20is%20character)%20or%20a%20matrix%20(if%20the%20column%20is%20numeric)%3C%2FSPAN%3E%3C%2FLI%3E%3CLI%3Eturn%20that%20list%20or%20martrix%20into%20an%20associative%20array%3C%2FLI%3E%3CLI%3Eget%20the%20keys%20for%20the%20associative%20array%2C%20which%20in%20this%20case%20are%20the%20unique%20levels%20of%20the%20values%20in%20the%20column%2C%20returning%20that%20as%20a%20list%3C%2FLI%3E%3CLI%3E(if%20Matrix()%20is%20used%20on%20the%20resulting%20list%2C%20it%20will%20only%20work%20if%20the%20values%20in%20the%20list%20are%20all%20numbers).%3C%2FLI%3E%3C%2FUL%3E%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-220875%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Get%20a%20list%20of%20the%20unique%20values%20in%20a%20column%2C%20matrix%2C%20or%20list%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-220875%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ESummarize()%20is%20way%20much%20more%20efficient%20than%20associative%20array%2C%20it%20is%20very%20obvious%20with%202%20million%20rows%20of%20data%20(1sec%20vs%208sec%20time%20lapse).%20If%20performance%20is%20critical%2C%20use%20Summarize()%20whenever%20possible.%3C%2FP%3E%3CP%3ESummarize(%20dt%2C%20smrz1%20%3D%20by(%20%3Asex%20)%20)%3B%20Summarize(%20dt%2C%20smrz2%20%3D%20by(%20%3Aage%20)%20)%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E*%20Summarize()%20does%20not%20create%20intermediate%20data%20table%20as%20%26lt%3B%26lt%3B%20Summary%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAssociative%20Array%20is%20great%20in%20the%20way%20that%20it%20saves%20the%20effort%20to%20put%20the%20list%2Fmatrix%20into%20data%20table%2C%20this%20is%20perfect%20solution%20for%20small%20data%20size.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
Get a list of the unique values in a column, matrix, or list
Problem

Problem 1:  I need to get a list of the unique values in a data table column.

Problem 2:  I need to get a list of the unique values in a JSL data structure (list or matrix)

Solution

For problem 1:

Solution 1:  Use summary tables.  The JSL below shows how to use Summary() to get a table that will have a row with each unique value in a single column.   You can then do further JSL table commands to get those values from the summary table. 

 

_dt = Open( "$sample_data/big class.jmp" );
//create a summary table of the :sex column
_dt1 = _dt << Summary(
	Group( :sex ),
	Freq( "None" ),
	Weight( "None" )
);
//create a summary table of the :age column
_dt2 = _dt << Summary(
	Group( :age ),
	Freq( "None" ),
	Weight( "None" )
);

2-13-2018 5-22-08 PM.png

 
Problem 2:  
Now,  instead of a data table you have a JMP list or matrix:
sexlist = {"F", "M", "M", "F", "F", "M", "M", "F", "M"};
agelist = [14, 11, 12, 13, 15, 17, 15, 11, 12];
the complicated ways to do this would be:
a) put the values in the list into a new data table column, and then use the summary approach, OR
b) write a somewhat complicated for loop to search for and record unique values
 
Fortunately, there is an easier way, using associative arrays.  
Associative Array(sexlist) << Get Keys;
Associative Array(agelist) << Get Keys;
returns {"F", "M"} and {11, 12, 13, 14, 15, 17}, respectively (note that these are nice sorted list!).  If you would like the numeric values as a sorted vector, you can use
Matrix(Associative Array(agelist) << Get Keys);
which returns [11, 12, 13, 14, 15, 17].  
 
This approach can also be used in place of Solution 1 for Problem 1:
 
Problem1, Solution 2:
Associative Array(Column(_dt, "sex") << Get Values) << Get Keys;
Matrix(Associative Array(Column(_dt, "age")<< Get Values)<<Get Keys);
returns {"F", "M"} and [11, 12, 13, 14, 15, 17]., respectively. What this is doing is 
  • get the values from the specified column as a list (if the column is character) or a matrix (if the column is numeric)
  • turn that list or martrix into an associative array
  • get the keys for the associative array, which in this case are the unique levels of the values in the column, returning that as a list
  • (if Matrix() is used on the resulting list, it will only work if the values in the list are all numbers).
 
Comments
HP_Lau

Summarize() is way much more efficient than associative array, it is very obvious with 2 million rows of data (1sec vs 8sec time lapse). If performance is critical, use Summarize() whenever possible.

Summarize( dt, smrz1 = by( :sex ) ); Summarize( dt, smrz2 = by( :age ) );

* Summarize() does not create intermediate data table as << Summary

 

Associative Array is great in the way that it saves the effort to put the list/matrix into data table, this is perfect solution for small data size.

 

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.