This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

GatherBy

GatherBy
gathers into sublists each set of elements in list that gives the same value when f is applied.
GatherBy
gathers list into nested sublists using at level i.
  • GatherBy evaluates for each i to determine how to gather the .
  • GatherBy gives results that follow the ordering of elements in list.
  • Within each sublist generated by GatherBy, elements appear in the same order as in list; the complete sublists are ordered so that their first elements appear in the same order as in list.
  • GatherBy effectively produces an equivalence partition.
Gather data in odd and even lists:
Gather by remainder:
Gather by the first component in a vector:
Gather dates by year:
Gather by the first component and, within that, gather by the last component:
Use Framed to display grouping:
Gather data in odd and even lists:
In[1]:=
Click for copyable input
Out[1]=
 
Gather by remainder:
In[1]:=
Click for copyable input
Out[1]=
 
Gather by the first component in a vector:
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
 
Gather dates by year:
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
 
Gather by the first component and, within that, gather by the last component:
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
Use Framed to display grouping:
In[3]:=
Click for copyable input
Out[3]=
Simulate some data:
Group elements based on the value of the second element:
Count the number of data points in each group:
Compute the means of the third element by group:
Group machine numbers by equivalence class based on rounding off the last tol bits:
Generate an unordered sample of distinct machine numbers close to 1:
Gather by equivalence class:
See how the factors in were grouped:
Here are the equivalence representatives and the values such that :
GatherBy[list] is equivalent to Gather[list]:
GatherBy is equivalent to Gather:
Gather the rows by Norm and display with the common norm of each group:
Compare to the result of Gather:
New in 7