cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Sign-in to the JMP Community will be unavailable intermittently Dec. 6-7 due to a system update. Thank you for your understanding!
  • We’re retiring the File Exchange at the end of this year. The JMP Marketplace is now your destination for add-ins and extensions.
  • JMP 19 is here! Learn more about the new features.

Discussions

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

Count the Most Frequently Occurring Item in a Column

I want to readily identify the item that most frequently occurs by row count in a JMP column.  I can easily determine the unique entries, how best can I get a count from there?

 

dt = Current Data Table();
summarize( lotz = by(:LOT));

 

 


Slán



SpannerHead
2 ACCEPTED SOLUTIONS

Accepted Solutions
jthi
Super User

Re: Count the Most Frequently Occurring Item in a Column

Summary Platform is what I generally use for this (how I use it depends on the use case: ties, do I need the count, how should missing values be handled and so on)

 

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");

dt_summary = dt << Summary(
	Group(:sex),
	Freq("None"),
	Weight("None"),
	output table name("res"),
	Private
);

// Using Loc Max
max_row = Loc Max(dt_summary[0, "N Rows"]);
max_val = dt_summary[max_row, 1];
show(max_val);

// Sorting
dt_summary << Sort(By(:N Rows), Replace Table, Order(Descending));
my_val = dt_summary[1, 1];
show(my_val);

Close(dt_summary, no save);

Also just using Mode function might be enough

 

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");
val = Mode(dt[0, "sex"]);

And there are plenty of other options (like you can see in this thread already).

 

-Jarmo

View solution in original post

SpannerHead
Level VI

Re: Count the Most Frequently Occurring Item in a Column

I made this tweak and I appear to be successful.

 

dt = Current Data Table();

full_proc_rows = dt << get rows where( !Is Missing( :process ) );

proc_val = Mode(dt[full_proc_rows, "process"]);

Slán



SpannerHead

View solution in original post

9 REPLIES 9

Re: Count the Most Frequently Occurring Item in a Column

This takes into account the possibility of a tie for most frequent:

Names Default To Here( 1 );
dt = Data Table( "Big Class" );
dt << Add Rows( {:name = "BARBARA", :age = 13, :sex = "F", :height = 100, :weight = 79} );
Summarize( namez = By( :name ) );
numberz = Transform Each( {v, i}, namez, Length( Where( dt, :name == v ) ) );
max_num = Max( numberz );	//2
big_name = namez[Where( numberz == max_num )];	//{"BARBARA", "ROBERT"}
SpannerHead
Level VI

Re: Count the Most Frequently Occurring Item in a Column

This is a good looking script but for some reason, it gives numberz as 5 for everything.

 

Thanks

 


Slán



SpannerHead
txnelson
Super User

Re: Count the Most Frequently Occurring Item in a Column

The Distribution Platform will give you a Mode.

txnelson_0-1745508303449.png

It is under the red triangle for the Summary Statistics Paragraph.  Just select

     Customize Summary Statistics

txnelson_1-1745508397071.png

and then select the additional statistics you want displayed

txnelson_2-1745508462827.png

 

 

Jim
hogi
Level XIII

Re: Count the Most Frequently Occurring Item in a Column

If you prefer a column formula:

Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Big Class Families.jmp" );
New Column( "N by age",
	Formula( Col Number( 1, :age ) )
)

 

 

and to identify the winner:

New Column( "max N by age",
	Formula( Col Number( 1, :age ) == Col Max( Col Number( 1, :age ) ) )
)

 

 

Alternatively, you can use Summarize:

Summarize( namez = By( :age ), cnt = count(:age) );

 

or tables/summary:

Data Table( "Big Class Families" ) << Summary(
	Group( :age )
);

 

SpannerHead
Level VI

Re: Count the Most Frequently Occurring Item in a Column

The summarize suggestion is a good one.  It gives me 2 dissociated lists.  If I could somehow get that to be an associative array, I could use the max "cnt" value to identify the associated "namez" value.


Slán



SpannerHead
SpannerHead
Level VI

Re: Count the Most Frequently Occurring Item in a Column

I did this and it seems to work.

 

Summarize( namez = By( :LOT ), cnt = count(:LOT) );

keys = namez;
values = Eval List(cnt);

AA = Associative Array(keys, values);

For( g = 1, g <= N Items( AA ), g++,
    If(values[g] == Max(cnt), mainLOT = keys[g]));

Slán



SpannerHead
jthi
Super User

Re: Count the Most Frequently Occurring Item in a Column

Summary Platform is what I generally use for this (how I use it depends on the use case: ties, do I need the count, how should missing values be handled and so on)

 

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");

dt_summary = dt << Summary(
	Group(:sex),
	Freq("None"),
	Weight("None"),
	output table name("res"),
	Private
);

// Using Loc Max
max_row = Loc Max(dt_summary[0, "N Rows"]);
max_val = dt_summary[max_row, 1];
show(max_val);

// Sorting
dt_summary << Sort(By(:N Rows), Replace Table, Order(Descending));
my_val = dt_summary[1, 1];
show(my_val);

Close(dt_summary, no save);

Also just using Mode function might be enough

 

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");
val = Mode(dt[0, "sex"]);

And there are plenty of other options (like you can see in this thread already).

 

-Jarmo
SpannerHead
Level VI

Re: Count the Most Frequently Occurring Item in a Column

Jarmo

 

Mighty!  This does the trick and it includes non numeric values.  The only refinement I need is to have it ignore missing data somehow?

 

dt = Current Data Table();
val = Mode(dt[0, "process"]);

 

 


Slán



SpannerHead
SpannerHead
Level VI

Re: Count the Most Frequently Occurring Item in a Column

I made this tweak and I appear to be successful.

 

dt = Current Data Table();

full_proc_rows = dt << get rows where( !Is Missing( :process ) );

proc_val = Mode(dt[full_proc_rows, "process"]);

Slán



SpannerHead

Recommended Articles