Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Create a rectangular data structure that behaves like a SparseArray indexed by string keys
ResourceFunction["SparseAssociation"][array,{key1,key2,…}] creates a data structure in which an element at position {i,j,…} can be accessed with the string key sequence {keyi, keyj, …}. | |
ResourceFunction["SparseAssociation"][array,{{key1,1,key1,2,…},{key2,1,…},…}] creates a data stucture that uses the keys keyi,j at level j of the array. | |
ResourceFunction["SparseAssociation"][array,keys,default] uses default as the default value for the underlying SparseArray. | |
ResourceFunction["SparseAssociation"][{keys1→val1,keys2→val2,…},Automatic,…] specifies the data from array rules. | |
ResourceFunction["SparseAssociation"][{keys1,keys2,…}→ {val1,val2,…},Automatic,…] can also be used. | |
ResourceFunction["SparseAssociation"][assoc,Automatic,…] specifies the data from a nested Association. |
Convert a regular array into a SparseAssociation:
In[1]:= | ![]() |
Out[1]= | ![]() |
In[2]:= | ![]() |
Out[2]= | ![]() |
Extract elements from it:
In[3]:= | ![]() |
Out[3]= | ![]() |
This is equivalent to:
In[4]:= | ![]() |
Out[4]= | ![]() |
Unlike Association, you cannot extract keys that haven’t been specified beforehand:
In[5]:= | ![]() |
Out[5]= | ![]() |
SparseAssociation works with Part:
In[6]:= | ![]() |
Out[6]= | ![]() |
Extracting multiple elements with Part yields another SparseAssociation of reduced size:
In[7]:= | ![]() |
Out[7]= | ![]() |
Indexing by position is also possible:
In[8]:= | ![]() |
Out[8]= | ![]() |
Convert the SparseAssociation to a SparseArray:
In[9]:= | ![]() |
Out[9]= | ![]() |
Further convert the SparseArray to a nested list using Normal:
In[10]:= | ![]() |
Out[10]= | ![]() |
Convert the SparseAssociation to array rules:
In[11]:= | ![]() |
Out[11]= | ![]() |
Convert the sparse association directly to a nested Association:
In[12]:= | ![]() |
Out[12]= | ![]() |
Convert it to a ragged dataset with the default elements omitted:
In[13]:= | ![]() |
Out[13]= | ![]() |
Convert the sparse association to a nested Association with the default elements omitted:
In[14]:= | ![]() |
Out[14]= | ![]() |
Reassemble the SparseAssociation from the nested Association (up to permutation of the keys):
In[15]:= | ![]() |
Out[15]= | ![]() |
Construct a SparseAssociation from a SparseArray:
In[16]:= | ![]() |
Out[16]= | ![]() |
Define a SparseAssociation using an array of rules:
In[17]:= | ![]() |
Out[18]= | ![]() |
Use Keys to extract the keys used at the different levels. The ordering of the keys depends on their appearance in the rules:
In[19]:= | ![]() |
Out[19]= | ![]() |
Specify the ordering of the keys in advance:
In[20]:= | ![]() |
Out[20]= | ![]() |
Specify different orderings at different levels:
In[21]:= | ![]() |
Out[21]= | ![]() |
Use a default element other than 0 and specify keys that do not appear in the list of rules:
In[22]:= | ![]() |
Out[22]= | ![]() |
Keys without a specified value will yield the default value:
In[23]:= | ![]() |
Out[23]= | ![]() |
Specify different keys at different levels:
In[24]:= | ![]() |
Out[24]= | ![]() |
The functions Length, Dimensions, ArrayDepth, MatrixQ, VectorQ and ArrayQ are supported:
In[25]:= | ![]() |
Out[25]= | ![]() |
In[26]:= | ![]() |
Out[26]= | ![]() |
Map is supported for operations that preserve the dimensions of the array:
In[27]:= | ![]() |
Out[27]= | ![]() |
In[28]:= | ![]() |
Out[28]= | ![]() |
Construct a SparseAssociation from nested associations:
In[29]:= | ![]() |
Out[29]= | ![]() |
Missing values at any level will be dropped, but the keys will be retained:
In[30]:= | ![]() |
Out[30]= | ![]() |
In[31]:= | ![]() |
Out[31]= | ![]() |
A SparseAssociation is more memory efficient than a nested Association because the keys do not have to be stored multiple times on each level:
In[32]:= | ![]() |
Out[32]= | ![]() |
In[33]:= | ![]() |
Out[33]= | ![]() |
A SparseAssociation can be reconstructed from its array rules and its keys:
In[34]:= | ![]() |
Out[34]= | ![]() |
In[35]:= | ![]() |
Out[35]= | ![]() |
It can also be reconstructed from its underlying SparseArray and its keys:
In[36]:= | ![]() |
Out[36]= | ![]() |
It can be reconstructed from the corresponding nested Association if the correct default value is provided:
In[37]:= | ![]() |
Out[37]= | ![]() |
Test if a SparseAssociation has been constructed correctly:
In[38]:= | ![]() |
Out[38]= | ![]() |
It is not possible to mix keys and position specifications at one level:
In[39]:= | ![]() |
Out[40]= | ![]() |
This is analogous to the same limitation of Association:
In[41]:= | ![]() |
Out[41]= | ![]() |
Use only strings or only integers instead:
In[42]:= | ![]() |
Out[42]= | ![]() |
In[43]:= | ![]() |
Out[43]= | ![]() |
At different levels, different specifications can be used:
In[44]:= | ![]() |
Out[44]= | ![]() |
Excel files often have row and column headers. SparseAssociation provides an easy mechanism to create a lookup table from such data:
In[45]:= | ![]() |
Out[46]= | ![]() |
In[47]:= | ![]() |
Out[47]= | ![]() |
The data can now be queried like an Association:
In[48]:= | ![]() |
Out[48]= | ![]() |
Convert to a Dataset:
In[49]:= | ![]() |
Out[49]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License