Subscribe Bookmark RSS Feed

JSL: get unique values of a data table column

Hi,

I currently use the summary command to get unique values of a data table column, which in my opinion is too complicated for that simple task. I think there has to be a simpler command that works on a data table column or a vector, but I cannot find one. Am I right?

My current code looks like this:


1 ACCEPTED SOLUTION

Accepted Solutions
ms

Super User

Joined:

Jun 23, 2011

Solution

Try summarize... 

Summarize can do more...see the scripting docs.

 

A couple of downsides.

  1. Even though the column summarized may be numeric, the list returned contains character values. 
  2. Related to 1, the precision of numeric values gets truncated based on the column width.

 

// sample data set:
dt = newtable("dt");
dt:"Column 1" << setvalues([1, 1, 2, 7, 7, 4, 5, 1, 5]);  summarize(unique_values=by(Column 1));
show(unique_values)

But ... here's a workaround to the downsides:

summarize(unique_values=by(Column 1), unique_numeric_values=(max(Column 1)));
show(unique_values, unique_numeric_values) 

 

4 REPLIES
Thanks for your help! Summarize works fine!

Dahla
markbailey

Staff

Joined:

Jun 23, 2011

Also:

unique values = Associative Array( col ) << Get Keys
Learn it once, use it forever!
uday_guntupalli

Community Trekker

Joined:

Sep 15, 2014

Summarize works well if you want to keep the data in a data table format .

Associative Array as suggested by @markbailey works well if you want to work in lists .
Best
Uday
ms

Super User

Joined:

Jun 23, 2011

Solution

Try summarize... 

Summarize can do more...see the scripting docs.

 

A couple of downsides.

  1. Even though the column summarized may be numeric, the list returned contains character values. 
  2. Related to 1, the precision of numeric values gets truncated based on the column width.

 

// sample data set:
dt = newtable("dt");
dt:"Column 1" << setvalues([1, 1, 2, 7, 7, 4, 5, 1, 5]);  summarize(unique_values=by(Column 1));
show(unique_values)

But ... here's a workaround to the downsides:

summarize(unique_values=by(Column 1), unique_numeric_values=(max(Column 1)));
show(unique_values, unique_numeric_values)