Function Repository Resource:

Proportions

Source Notebook

Get the proportion of times that each distinct element appears in a list

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["Proportions"][list]

gives an association whose keys are the distinct elements of list, and whose values give the proportion of times those elements appear in list.

Details and Options

ResourceFunction["Proportions"][list] gives an association whose keys are in the same order as they first occur as elements of list.
ResourceFunction["Proportions"] also works with heads other than List.
When given an Association, ResourceFunction["Proportions"] uses the Values of the association as input.

Examples

Basic Examples (4) 

Find the proportions of times that each element appears in a list:

In[1]:=
ResourceFunction["Proportions"][{a, b, c, a}]
Out[1]=

Use with an Association:

In[2]:=
ResourceFunction["Proportions"][<|a -> x, b -> y, c -> z, d -> y|>]
Out[2]=

See how common each character is in a body of text:

In[3]:=
ResourceFunction["Proportions"][
   Characters[ResourceData["Gettysburg Address"]]] // ReverseSort // N
Out[3]=

Check for common words:

In[4]:=
p = TakeLargest[
  N[ResourceFunction["Proportions"][
    TextWords[ResourceData["On the Origin of Species"]]]], 20]
Out[4]=
In[5]:=
BarChart[Reverse[p], ChartLabels -> Automatic, BarOrigin -> Left]
Out[5]=

Visualize in a WordCloud:

In[6]:=
WordCloud[p]
Out[6]=

Properties and Relations (1) 

Proportions is similar to Counts:

In[7]:=
ResourceFunction["Proportions"][{a, b, c, a}]
Out[7]=
In[8]:=
Counts[{a, b, c, a}]
Out[8]=
In[9]:=
%/4
Out[9]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 28 March 2019

Related Resources

License Information