WOLFRAM

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 effectively produces an equivalence partition.
  • GatherBy[list,{f1,f2}] is equivalent to Map[GatherBy[#,f2]&,GatherBy[list,f1]].

Examples

open allclose all

Basic Examples  (2)Summary of the most common use cases

Gather data in odd and even lists:

Out[1]=1

Gather by the first part:

Out[1]=1

Applications  (6)Sample problems that can be solved with this function

Sort and then gather strings by their first letters:

Gather by the first component, and within that group, gather by the last component:

Out[1]=1
Out[2]=2

Use Framed to display grouping:

Out[3]=3

Gather by remainder:

Out[1]=1

Gather by the first component in a vector:

Out[1]=1
Out[2]=2

Gather dates by year:

Out[1]=1
Out[2]=2

Simulate some data:

Out[3]=3

Group elements based on the value of the second element:

Out[4]=4

Count the number of data points in each group:

Out[5]=5

Compute the means of the third element by group:

Out[6]=6

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:

Out[2]=2

Gather by equivalence class:

Out[3]=3

See how the factors in were grouped:

Out[4]=4

Here are the equivalence representatives and the values such that :

Out[5]=5

Properties & Relations  (1)Properties of the function, and connections to other functions

GatherBy[list,f] is equivalent to Gather[list,(f[#1]===f[#2])&]:

Gather the rows by Norm and display with the common norm of each group:

Out[2]=2

Compare to the result of Gather:

Out[3]=3
Wolfram Research (2008), GatherBy, Wolfram Language function, https://reference.wolfram.com/language/ref/GatherBy.html.
Wolfram Research (2008), GatherBy, Wolfram Language function, https://reference.wolfram.com/language/ref/GatherBy.html.

Text

Wolfram Research (2008), GatherBy, Wolfram Language function, https://reference.wolfram.com/language/ref/GatherBy.html.

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.

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

Wolfram Language. (2008). GatherBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GatherBy.html

BibTeX

@misc{reference.wolfram_2025_gatherby, author="Wolfram Research", title="{GatherBy}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/GatherBy.html}", note=[Accessed: 07-June-2025 ]}

@misc{reference.wolfram_2025_gatherby, author="Wolfram Research", title="{GatherBy}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/GatherBy.html}", note=[Accessed: 07-June-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_gatherby, organization={Wolfram Research}, title={GatherBy}, year={2008}, url={https://reference.wolfram.com/language/ref/GatherBy.html}, note=[Accessed: 07-June-2025 ]}

@online{reference.wolfram_2025_gatherby, organization={Wolfram Research}, title={GatherBy}, year={2008}, url={https://reference.wolfram.com/language/ref/GatherBy.html}, note=[Accessed: 07-June-2025 ]}