Basic Examples (14)
Convert a regular array into a SparseAssociation:
Extract elements from it:
This is equivalent to:
Unlike Association, you cannot extract keys that haven’t been specified beforehand:
SparseAssociation works with Part:
Extracting multiple elements with Part yields another SparseAssociation of reduced size:
Indexing by position is also possible:
Convert the SparseAssociation to a SparseArray:
Further convert the SparseArray to a nested list using Normal:
Convert the SparseAssociation to array rules:
Convert the sparse association directly to a nested Association:
Convert it to a ragged dataset with the default elements omitted:
Convert the sparse association to a nested Association with the default elements omitted:
Reassemble the SparseAssociation from the nested Association (up to permutation of the keys):
Scope (6)
Construct a SparseAssociation from a SparseArray:
Define a SparseAssociation using an array of rules:
Use Keys to extract the keys used at the different levels. The ordering of the keys depends on their appearance in the rules:
Specify the ordering of the keys in advance:
Specify different orderings at different levels:
Use a default element other than 0 and specify keys that do not appear in the list of rules:
Keys without a specified value will yield the default value:
Specify different keys at different levels:
The functions Length, Dimensions, ArrayDepth, MatrixQ, VectorQ and ArrayQ are supported:
Map is supported for operations that preserve the dimensions of the array:
Construct a SparseAssociation from nested associations:
Missing values at any level will be dropped, but the keys will be retained:
Applications (1)
A SparseAssociation is more memory efficient than a nested Association because the keys do not have to be stored multiple times on each level:
Properties and Relations (4)
A SparseAssociation can be reconstructed from its array rules and its keys:
It can also be reconstructed from its underlying SparseArray and its keys:
It can be reconstructed from the corresponding nested Association if the correct default value is provided:
Test if a SparseAssociation has been constructed correctly:
Possible Issues (4)
It is not possible to mix keys and position specifications at one level:
This is analogous to the same limitation of Association:
Use only strings or only integers instead:
At different levels, different specifications can be used:
Neat Examples (3)
Excel files often have row and column headers. SparseAssociation provides an easy mechanism to create a lookup table from such data:
The data can now be queried like an Association:
Convert to a Dataset: