Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Create associations among columns of a tabular object
ResourceFunction["AssociateColumns"][tab,{col1,col2}] associates one column of tab to another column. | |
ResourceFunction["AssociateColumns"][tab,{col1,col2},f] specify a merging function for possible duplicate keys. | |
ResourceFunction["AssociateColumns"][tab,{{col1,col2,…},{col3,col4,…}}] associates multiple columns as keys to multiple columns as values. | |
ResourceFunction["AssociateColumns"][tab,{{col1,col2,…},{col3,col4,…}},f] specify a merging function. | |
ResourceFunction["AssociateColumns"][tab,{cols1,cols2,cols3,…}] creates an arbitrarily nested Association. | |
ResourceFunction["AssociateColumns"][tab,cols1→cols2→cols3→…] alternative syntax with a rule for each level of nesting. | |
ResourceFunction["AssociateColumns"][tab,cols1→cols2→cols3→…,f] specify the same merging function for each nesting level. | |
ResourceFunction["AssociateColumns"][tab,cols1→cols2→cols3→…,{f12,f23,…}] specify a different merging function for each nesting level. |
| "DuplicatesWarning" | False | whether to output a warning message if the column of the keys contains duplicates |
Create a Tabular expression:
| In[1]:= |
| Out[1]= | ![]() |
Create an association between two columns of the Tabular:
| In[2]:= |
| Out[2]= | ![]() |
Create a nested association with three columns:
| In[3]:= |
| Out[3]= | ![]() |
If the first column contains duplicate keys, you can choose to merge them through a function specified as third argument:
| In[4]:= |
| Out[4]= | ![]() |
Use an alternative syntax with a Rule for each level of nesting:
| In[5]:= |
| Out[5]= | ![]() |
| In[6]:= |
| Out[6]= | ![]() |
Create an association with multiple columns as Values:
| In[7]:= |
| Out[7]= | ![]() |
| In[8]:= |
| Out[8]= | ![]() |
Use multiple columns as Keys:
| In[9]:= |
| Out[9]= | ![]() |
| In[10]:= |
| Out[10]= |
| In[11]:= |
| Out[11]= | ![]() |
Create an arbitrarily nested association and visualize it as a Dataset:
| In[12]:= | ![]() |
| In[13]:= | ![]() |
| Out[13]= | ![]() |
| In[14]:= |
| Out[14]= | ![]() |
Specify a different merging function for each level of nesting:
| In[15]:= | ![]() |
| Out[15]= |
Return a warning message when the column of the Keys contains duplicates and no merging function is specified:
| In[16]:= |
| Out[16]= | ![]() |
| In[17]:= |
| Out[17]= | ![]() |
To avoid this message, you can just specify a merging function as the third argument, or set the option "DuplicatesWarning" to False, to obtain the automatic merging through Identity without error messages:
| In[18]:= |
| Out[18]= | ![]() |
| In[19]:= |
| Out[19]= | ![]() |
| In[20]:= |
| Out[20]= |
Create an association between the coordinates and magnitudes of earthquakes:
| In[21]:= |
| Out[21]= | ![]() |
| In[22]:= | ![]() |
| Out[22]= |
Use it to create a GeoHistogram of US seismicity:
| In[23]:= |
| Out[23]= | ![]() |
In the simplest case with only 2 columns, if there are no duplicates in the column of the keys, AssociateColumn[tab,{col1,col2}] is equivalent to Association[Rule@@@Values@Normal@tab[[All,{col1,col2}]]]:
| In[24]:= |
| Out[24]= | ![]() |
| In[25]:= |
| Out[25]= | ![]() |
| In[26]:= |
| Out[26]= | ![]() |
| In[27]:= |
| Out[27]= |
If there are duplicates in the column of the keys, AssociateColumns[tab, {col1,col2},f] is equivalent to Map[f,GroupBy[Values@Normal@tab[[All,{col1,col2}]],First→Last]]:
| In[28]:= |
| Out[28]= | ![]() |
| In[29]:= |
| Out[29]= | ![]() |
| In[30]:= |
| Out[30]= |
The merging functions should be actually allowed by the structure of the data:
| In[31]:= | ![]() |
| In[32]:= | ![]() |
| Out[32]= |
| In[33]:= | ![]() |
| Out[33]= |
| In[34]:= | ![]() |
| Out[34]= | ![]() |
Sometimes the error messages suggest what to change:
| In[35]:= |
| Out[35]= | ![]() |
| In[36]:= |
| Out[36]= | ![]() |
| In[37]:= |
| Out[37]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License