Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > List Manipulation > Rearranging & Restructuring Lists >
Mathematica > Data Manipulation > Handling Arrays of Data > Rearranging & Restructuring Lists >

GatherBy

GatherBy[list, f]
gathers into sublists each set of elements in list that gives the same value when f is applied.
GatherBy[list, {f1, f2, ...}]
gathers list into nested sublists using fi at level i.
  • GatherBy[{e1, e2, e3, ...}, f] evaluates f[ei] for each i to determine how to gather the ei.
  • GatherBy[list, ...] gives results that follow the ordering of elements in list.
  • Within each sublist generated by GatherBy[list, ...], 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.
  • GatherBy[list, {f1, f2}] is equivalent to Map[GatherBy[#, f2]&, GatherBy[list, f1]].
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 k in x=k epsilon+1.` were grouped:
Here are the equivalence representatives r and the values k such that r=k epsilon+1:
GatherBy[list] is equivalent to Gather[list]:
GatherBy[list, f] is equivalent to Gather[list, (f[#1]SameQf[#2])&]:
Gather the rows by Norm and display with the common norm of each group:
Compare to the result of Gather:
New in 7
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team