# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

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

Contributed by:
Sander Huisman

ResourceFunction["BinListsBy"][{ gives lists of the elements | |

ResourceFunction["BinListsBy"][{ gives lists of the elements | |

ResourceFunction["BinListsBy"][{ gives an array of lists of the elements |

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 .

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]= |

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]= |

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]= |

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]= |

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]= |

Bin some datasets based on their regressed slope:

In[40]:= |

Out[42]= |

Plot the datasets with similar slopes together:

In[43]:= |

Out[43]= |

Wolfram Language 11.3 (March 2018) or above

- 1.0.0 – 01 April 2019

This work is licensed under a Creative Commons Attribution 4.0 International License