Function Repository Resource:

# BinListsBy

Bin data into lists based on applying a function to each item

Contributed by: Sander Huisman
 ResourceFunction["BinListsBy"][{x1,x2,…},{f,xmin,xmax}] gives lists of the elements xi for which the values of f[xi] lie in bins from xmin to xmax with unit width. ResourceFunction["BinListsBy"][{x1,x2,…},{f,xmin,xmax,Δx}] gives lists of the elements xi for which the values of f[xi] lie in bins from xmin to xmax with width Δx. ResourceFunction["BinListsBy"][{x1,x2,…},binspec1,binspec2,…] gives an array of lists of the elements xi for which each index is determined by applying fi of the binning specifications binspeci to the value xi.

## Details and Options

ResourceFunction["BinListsBy"] drops those elements for which the function evaluates to values that are outside the binning specification.
ResourceFunction["BinListsBy"] drops those elements for which the function evaluates to complex numbers or to non-numerical data.
Within each bin, elements appear in the same order as in the original data.
ResourceFunction["BinListsBy"] places elements in bin i when their function values satisfy .

## Examples

### Basic Examples (4)

Bin some data based only on the first element of each list:

 In[1]:=
 Out[2]=

Bin a list of data based on the string length of each item:

 In[3]:=
 Out[4]=

Bin some data based on the total of each list:

 In[5]:=
 Out[6]=

Bin some data with some "auxiliary" data:

 In[7]:=
 Out[8]=

### Scope (3)

Perform a 2-dimensional binning based on the value of the first element, and the string length of the last element:

 In[9]:=
 Out[11]=

Perform a 3-dimensional binning based on the value of the first element, the value of the middle element, and the length of the last element:

 In[12]:=
 Out[13]=

The lists do not need to have the same length, structure, or type, allowing to bin data with auxiliary data:

 In[14]:=
 Out[15]=

### Applications (5)

Bin some data based on the number of unique letters in each string:

 In[16]:=
 Out[17]=

Bin complex numbers based on their imaginary and real parts:

 In[18]:=
 Out[21]=

Bin some strings based on their first letter:

 In[22]:=
 Out[24]=

Bin some person entities based on their birth century:

 In[25]:=
 Out[27]=

Bin some associations based on age:

 In[28]:=
 Out[29]=

### Properties and Relations (2)

BinLists can sometimes perform similar tasks as BinListsBy by giving a single large bin in the "other" dimensions:

 In[30]:=
 Out[31]=

GatherBy gives similar output as BinListsBy, but BinListsBy always returns an output with the same dimensions, the bins are sorted, and includes empty lists where necessary:

 In[32]:=
 In[33]:=
 Out[33]=
 In[34]:=
 Out[34]=

Explicitly check they return the same:

 In[35]:=
 Out[35]=

### Possible Issues (2)

If the function does not return a number it will not be binned:

 In[36]:=
 Out[37]=

Values outside the binning range are discarded:

 In[38]:=
 Out[39]=

### Neat Examples (2)

Bin some datasets based on their regressed slope:

 In[40]:=
 Out[42]=

Plot the datasets with similar slopes together:

 In[43]:=
 Out[43]=

SHuisman

## Requirements

Wolfram Language 11.3 (March 2018) or above

## Version History

• 1.0.0 – 01 April 2019