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.
Details
- 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[list] is equivalent to GatherBy[list,Identity], which is also equivalent to Gather[list]. »
- GatherBy effectively produces an equivalence partition.
- GatherBy[list,{f1,f2}] is equivalent to Map[GatherBy[#,f2]&,GatherBy[list,f1]].
Examples
open allclose allApplications (6)
Sort and then gather strings by their first letters:
Gather by the first component, and within that group, gather by the last component:
Use Framed to display grouping:
Gather by the first component in a vector:
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:
See how the factors in were grouped:
Here are the equivalence representatives and the values such that :
Text
Wolfram Research (2008), GatherBy, Wolfram Language function, https://reference.wolfram.com/language/ref/GatherBy.html.
CMS
Wolfram Language. 2008. "GatherBy." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/GatherBy.html.
APA
Wolfram Language. (2008). GatherBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GatherBy.html