Function Repository Resource:

MetaTally

Source Notebook

Combine multiple tally results

Contributed by: David Gathercole

ResourceFunction["MetaTally"][{t1,t2,}]

combines the counts of multiple tally results ti.

ResourceFunction["MetaTally"][{t1,t2,},f]

applies f to the list of counts of each given element, where by default these lists are totaled.

Details and Options

By way of an extension, ResourceFunction["MetaTally"] combines all but the first element of the second-level lists.
The default aggregation function is Total.

Examples

Basic Examples (2) 

MetaTally combines multiple tallies:

In[1]:=
ResourceFunction["MetaTally"][{Tally[{1, 1, 1, 2}], Tally[{1, 2, 2}]}]
Out[1]=

Analogous to the Tally of the combined lists:

In[2]:=
Tally[Join[{1, 1, 1, 2}, {1, 2, 2}]]
Out[2]=

Allowing work with intermediate tallies:

In[3]:=
catGenderTally = Tally[{"F", "F", "F", "F", "F", "M", "M", "M"}]
Out[3]=
In[4]:=
dogGenderTally = Tally[{"M", "M", "M", "M", "F"}]
Out[4]=
In[5]:=
ResourceFunction["MetaTally"][{catGenderTally, dogGenderTally}]
Out[5]=

Scope (2) 

The argument lists are compared on their first elements:

In[6]:=
ResourceFunction["MetaTally"][{
  {{{"crane", "noun"}, 2}, {{"crane", "verb"}, 2}, {{"hack", "verb"}, 7}},
  {{{"crane", "verb"}, 3}, {{"hack", "noun"}, 5}}
  }]
Out[6]=

The aggregation is performed on the rest of the elements:

In[7]:=
ResourceFunction["MetaTally"][{
   {{"crane", 2, 0}, {"crane", 0, 2}, {"hack", 0, 7}},
   {{"crane", 0, 3}, {"hack", 5, 0}}
   }] // TableForm
Out[7]=

The function for aggregation can be specified:

In[8]:=
ResourceFunction["MetaTally"][{
   {{"a", 0.3}, {"b", 0.6}, {"c", 0.1}},
   {{"a", 0.5}, {"b", 0.5}}
   }, Max] // TableForm
Out[8]=
In[9]:=
ResourceFunction["MetaTally"][{
  {{"a", 0.3}, {"b", 0.6}, {"c", 0.1}},
  {{"a", 0.5}, {"b", 0.5}}
  }, Apply[Times, #] &]
Out[9]=

Properties and Relations (1) 

The default aggregation function is Total:

In[10]:=
ResourceFunction["MetaTally"][{
  {{"a", 0.3}, {"b", 0.6}, {"c", 0.1}},
  {{"a", 0.5}, {"b", 0.5}}
  }]
Out[10]=
In[11]:=
ResourceFunction["MetaTally"][{
  {{"a", 0.3}, {"b", 0.6}, {"c", 0.1}},
  {{"a", 0.5}, {"b", 0.5}}
  }, Total]
Out[11]=

Publisher

David Gathercole

Version History

  • 1.0.0 – 28 June 2019

License Information