As a learning exercise I’m trying to convert some data we have from spreadsheets into an association of associations. However, I’ve only managed to format two different associations, a key of the element’s area and a list of elements in each group (1-49). I’ve tried things like ReplaceMap, KeyMap, and AssociationMap but I’m missing some logic for all these approaches to work. An association of associations seems like an excellent & natural way to structure this data but I can’t figure out how to do it. I’m trying to associate each element’s area to the matching element in the group so that further computation and manipulation can been done. I’d eventually like the ability to add other characteristics to each element type beyond just its area.

Element Key unitkey

<| S1-01->569, S1-02->518, S1-03->461.5, S1-04->570.6, S1-05->617.3, S1-06->631.4
, S1D-01->571.7, S1D-02->647.4,S1D-03->762.2,S1D-04->687.3,S1D-05->691.5,S1D-06->756.4,S1D-07->653.5,S1D-08->783.2,S1D-09->639.3
, S2-01->867.1,S2-02->809.3,S2-03->974.1,S2-04->826.4,S2-05->852,S2-06->852.5,S2-07->865.4,S2-08->865.2,S2-09->1023.4
,S2D-01->890, S2D-03->965.2,S2D-04->861.8,S2D-05->1028.9,S2D-06->986.1,S2D-08->1074.4,S2D-09->1062.6,S2D-10->1007.5
, TH-04->1488,TH-03->1424,TH-02->1352,TH-01->1312

Elements by Group unitsbylevel

<|1 -> {"TH-01", "TH-02", "TH-03", "TH-04"}, 
 2 -> {"S2D-09", "S1D-09", "S1-05", "S1-06"}, 
 3 -> {"S2-04", "S2D-09", "S1D-01", "S1-01", "S1-01", "S1D-03","S2D-05", "S2-03", "S2-06", "S2-07", "S2D-04"}, 
 4 -> {"S2-04", "S2D-09", "S1D-01", "S1-01", "S1-01", "S1D-03",        "S2D-05", "S2-03", "S2-06", "S2-07", "S2D-04"}, 
 5 -> {"S2-04", "S2D-09", "S1D-01", "S1-01", "S1-01", "S1D-03",        "S2D-05", "S2-03", "S2-06", "S2-07", "S2D-04"}, 
 6 -> {"S2-04", "S2D-09", "S1D-01", "S1-01", "S1-01", "S1D-03",        "S2D-05", "S2-03", "S2-06", "S2-07", "S2D-04"}, 
 7 -> {"S2-04", "S2D-09", "S1D-01", "S1-01", "S1-01", "S1D-03",        "S2D-05", "S2-03", "S2-06", "S2-07", "S2D-04"}, 
 8 -> {"S2-04", "S2D-09", "S1D-01", "S1-01", "S1-01", "S1D-03",        "S2D-05", "S2-03", "S2-06", "S2-07", "S2D-04"}, 
 9 -> {"S2-04", "S2D-09", "S1D-01", "S1-01", "S1-01", "S1D-03",        "S2D-05", "S2-03", "S2-06", "S2-07", "S2D-04"}, 
 10 -> {"S2-04", "S2D-09", "S1D-01", "S1-01", "S1-01", "S1D-03",      "S2D-05", "S2-03", "S2-06", "S2-07", "S2D-04"}, 
 11 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-08", "S2D-05",      "S2D-06", "S2-05", "S2D-01", "S2-08"}, 
 12 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-08", "S2D-05",      "S2D-06", "S2-05", "S2D-01", "S2-08"}, 
 13 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-08", "S2D-05",      "S2D-06", "S2-05", "S2D-01", "S2-08"}, 
 14 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-08", "S2D-05",      "S2D-06", "S2-05", "S2-08"}, 
 15 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 16 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 17 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
18 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 19 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 20 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 21 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 22 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 23 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 24 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 25 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 26 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 27 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 28 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 29 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 30 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 31 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 32 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
33 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 34 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 35 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 36 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 37 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 38 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 39 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 40 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 41 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 42 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 43 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 44 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 45 -> {"S1D-07", "S2-01", "S1-03", "S1-03", "S1D-06", "S1-04",       "S1D-04", "S1-02", "S1-02", "S2-02"}, 
 46 -> {"S2D-08", "S1D-02", "S2D-10", "S2-09", "S1D-05", "S2D-03"}, 
47 -> {"S2D-08", "S1D-02", "S2D-10", "S2-09", "S1D-05", "S2D-03"}, 
 48 -> {"S2D-08", "S1D-02", "S2D-10", "S2-09", "S1D-05", "S2D-03"}, 
 49 -> {"S2D-08", "S1D-02", "S2D-10", "S2-09", "S1D-05", "S2D-03"}

Update 2018-08-23

Added an example of what group 2 would theoretically look like.

<|2 -> <| S2D-09 ->  1062.6 , S1D-09 -> 639.3, S1-05->617.3, S1-06->631.4 |> |>
  • $\begingroup$ This might be semi-related: mathematica.stackexchange.com/questions/171104/…. $\endgroup$ – Carl Lange Aug 22 '18 at 19:51
  • $\begingroup$ I am not sure what is the expected result. Can you show a minimal example of input that comes from Import[file.xlsx] to work with? Can be 5x5x5. And a corresponding result? $\endgroup$ – Kuba Aug 23 '18 at 6:34
  • $\begingroup$ The two association lists above are from two different CSV files generated from Excel. I'm trying to merge them into a hierarchy. Groups > type and number of elements in each group > each specific element's area from the key. $\endgroup$ – BBirdsell Aug 23 '18 at 15:18
  • 1
    $\begingroup$ Lack of attention probably comes from the fact that <| S2D-09 -> 1062.6... is an invalid mma syntax, you are missing quotes around S2D-09: "S2D-09", so if you want anyone to play with your data you should provide a valid one. $\endgroup$ – Kuba Aug 27 '18 at 5:29
  • 1
    $\begingroup$ Agree w Kuba that you're shooting yourself in the foot by not wrapping the keys in your first association in quotes. Once that's done, it's a simple: map AssociationMap[elementKeyAssociation] on each of the lists of keys. $\endgroup$ – alancalvitti Aug 27 '18 at 16:11

I was able to get the exact structure I needed with the below quoted code. Not sure how best yet to visualize it or manipulate it but thats the fun part.

data = AssociationMap[Association[#->find[#] &/@ unitsbylevel[[#]]]&, Range[Length@unitsbylevel]];

Output: enter image description here

| improve this answer | |
  • $\begingroup$ unitsbylevel is not defined and it is not clear how 'elements by key' and 'elements by group' are used here making this answer useless for anyone but you. :-/ $\endgroup$ – Kuba Aug 27 '18 at 5:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.