GroupBy

GroupBy[{elem1,elem2,},f]
groups the into lists associated with distinct keys .

GroupBy[{elem1,elem2,},fkfv]
groups the according to the .

GroupBy[{elem1,elem2,},{fs1,fs2,}]]
groups into nested associations using at level i.

GroupBy[{elem1,elem2,},spec,red]
applies the function red to reduce lists of values that are generated.

GroupBy[spec]
represents an operator form of GroupBy that can be applied to an expression.

DetailsDetails

  • GroupBy provides a generalization of the map reduce operation.
  • GroupBy[spec][expr] is equivalent to GroupBy[expr,spec].

ExamplesExamplesopen allclose all

Basic Examples  (11)Basic Examples  (11)

Group pairs of items according to the first element of the pair:

In[1]:=
Click for copyable input
Out[1]=

Group pairs of items according to the last element:

In[2]:=
Click for copyable input
Out[2]=

Group the elements repeatedly by using several functions:

In[1]:=
Click for copyable input
Out[1]=

Group last parts of pairs according to the first part:

In[1]:=
Click for copyable input
Out[1]=

Use a symbolic selector for grouping:

In[1]:=
Click for copyable input
Out[1]=

Pairs for which the selector yields the same value are grouped under the same key:

In[2]:=
Click for copyable input
Out[2]=

Use different functions to extract keys and values:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=

Use the operator form of Extract to specify the key or the value:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Use a combiner function to combine the values:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Group associations according to the value of their first element:

In[1]:=
Click for copyable input
Out[1]=

Group associations according to the value of the given key:

In[2]:=
Click for copyable input
Out[2]=

Specify keys and values using Key:

In[1]:=
Click for copyable input
Out[1]=

Missing keys are replaced with Missing:

In[1]:=
Click for copyable input
Out[1]=

Use the operator form of GroupBy:

In[1]:=
Click for copyable input
Out[1]=
Introduced in 2014
(10.0)