Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Merge a list of associations using different merge functions for different keys
ResourceFunction["MergeByKey"][{assoc1,assoc2,…},{key1→f1,key2→f2,…}] merges the associations associ, using the the functions fi for combining the values of keys keyi. | |
ResourceFunction["MergeByKey"][{assoc1,assoc2,…},{key1→f1,key2→f2,…},fdefault] uses fdefault as the merging function for any key not specified. | |
ResourceFunction["MergeByKey"][{assoc1,assoc2,…},{…,{keyi,1,keyi,2,…}→fi,…},…] uses fi for merging keys keyi,j. | |
ResourceFunction["MergeByKey"][{key1 → f1, key2 → f2, …}] represents an operator form of ResourceFunction["MergeByKey"] that can be applied to an expression. | |
ResourceFunction["MergeByKey"][{key1 → f1, key2 → f2, …}, fdefault] gives an operator with a default merging function. |
Merge the values of keys a and b with different functions to combine the values:
In[1]:= | ![]() |
Out[1]= | ![]() |
Not all associations need to have the same keys:
In[2]:= | ![]() |
Out[2]= | ![]() |
Specify multiple keys that use the same merging function:
In[3]:= | ![]() |
Out[3]= | ![]() |
If no function is specified for a given key, all values are returned as a List:
In[4]:= | ![]() |
Out[4]= | ![]() |
Specify a default function for merging unspecified keys:
In[5]:= | ![]() |
Out[5]= | ![]() |
Define two operator forms and data:
In[6]:= | ![]() |
Apply them to the data:
In[7]:= | ![]() |
Out[7]= | ![]() |
In[8]:= | ![]() |
Out[8]= | ![]() |
Quickly summarize categorical and numerical columns of a dataset:
In[9]:= | ![]() |
Out[10]= | ![]() |
Turn the columns into distributions:
In[11]:= | ![]() |
Out[11]= | ![]() |
Draw samples from these distributions. Note that this is different from drawing a random row from the original dataset because it doesn't account for correlations between the columns:
In[12]:= | ![]() |
Out[12]= | ![]() |
Only specifying a default merging function is equivalent to using Merge:
In[13]:= | ![]() |
Out[13]= | ![]() |
In[14]:= | ![]() |
Out[14]= | ![]() |
For large datasets, it can be used as a faster alternative to Merge:
In[15]:= | ![]() |
Out[16]= | ![]() |
In[17]:= | ![]() |
Out[17]= | ![]() |
In[18]:= | ![]() |
Out[18]= | ![]() |
Merging an empty list returns an empty Association:
In[19]:= | ![]() |
Out[19]= | ![]() |
In[20]:= | ![]() |
Out[20]= | ![]() |
Similarly for a list of empty associations:
In[21]:= | ![]() |
Out[21]= | ![]() |
In[22]:= | ![]() |
Out[22]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License