1
$\begingroup$

This question already has an answer here:

Relating to a question I asked earlier at: Creating Association it was said that Merge[] is a more elegant way to associate data. I have an issue when I use to to concatenate associations I have shown bellow:

test1 = <|"GlobalFace" -> <|"NeuralNetwork" -> <|"Blink" -> 
    0.6666666666666666`, "Brow" -> 0.46153846153846156`, 
   "Control" -> 0.7804878048780489`, "Smile" -> 0.5`|>, 
 "LogisticRegression" -> <|"Blink" -> 0.6808510638297872`, 
   "Brow" -> 0.42105263157894735`, 
   "Control" -> 0.8292682926829269`, 
   "Smile" -> 0.5882352941176471`|>, 
 "SupportVectorMachine" -> <|"Blink" -> 0.631578947368421`, 
   "Brow" -> 0.6829268292682926`, 
   "Control" -> 0.7441860465116279`, 
   "Smile" -> 0.5789473684210527`|>, 
 "RandomForest" -> <|"Blink" -> 0.7804878048780489`, 
   "Brow" -> 0.8000000000000002`, 
   "Control" -> 0.8292682926829269`, 
   "Smile" -> 0.7368421052631577`|>, 
 "NearestNeighbors" -> <|"Blink" -> 0.6976744186046512`, 
   "Brow" -> 0.6976744186046512`, 
   "Control" -> 0.7804878048780489`, 
   "Smile" -> 0.6060606060606062`|>|>|>;

test2 = <|"Madeline Perault" -> <|"NeuralNetwork" -> <|"Blink" -> 
   0.9090909090909091`, "Brow" -> 0.5454545454545454`, 
  "Control" -> 1.`, "Smile" -> 0.5`|>, 
"LogisticRegression" -> <|"Blink" -> 0.8000000000000002`, 
  "Brow" -> 0.5454545454545454`, "Control" -> 0.8333333333333333`,
   "Smile" -> 0.5714285714285715`|>, 
"SupportVectorMachine" -> <|"Blink" -> 1.`, 
  "Brow" -> 0.7692307692307693`, "Control" -> 1.`, 
  "Smile" -> 0.5714285714285715`|>, 
"RandomForest" -> <|"Blink" -> 0.888888888888889`, 
  "Brow" -> 0.4000000000000001`, "Control" -> 0.8333333333333333`,
   "Smile" -> 0.4444444444444445`|>, 
"NearestNeighbors" -> <|"Blink" -> 0.7142857142857143`, 
  "Brow" -> 0.7692307692307693`, 
  "Control" -> 0.33333333333333337`, 
  "Smile" -> 0.5714285714285715`|>|>|>;

As it nests the association within the more broad association:

combineTest = Merge[{test1, test2}, Identity]

Giving me:

<|"GlobalFace" -> {<|"NeuralNetwork" -> <|"Blink" -> 0.666667, 
  "Brow" -> 0.461538, "Control" -> 0.780488, "Smile" -> 0.5|>, 
"LogisticRegression" -> <|"Blink" -> 0.680851, "Brow" -> 0.421053,
   "Control" -> 0.829268, "Smile" -> 0.588235|>, 
"SupportVectorMachine" -> <|"Blink" -> 0.631579, 
  "Brow" -> 0.682927, "Control" -> 0.744186, 
  "Smile" -> 0.578947|>, 
"RandomForest" -> <|"Blink" -> 0.780488, "Brow" -> 0.8, 
  "Control" -> 0.829268, "Smile" -> 0.736842|>, 
"NearestNeighbors" -> <|"Blink" -> 0.697674, "Brow" -> 0.697674, 
  "Control" -> 0.780488, "Smile" -> 0.606061|>|>}, 
"Madeline Perault" -> {<|"NeuralNetwork" -> <|"Blink" -> 0.909091, 
  "Brow" -> 0.545455, "Control" -> 1., "Smile" -> 0.5|>, 
"LogisticRegression" -> <|"Blink" -> 0.8, "Brow" -> 0.545455, 
  "Control" -> 0.833333, "Smile" -> 0.571429|>, 
"SupportVectorMachine" -> <|"Blink" -> 1., "Brow" -> 0.769231, 
  "Control" -> 1., "Smile" -> 0.571429|>, 
"RandomForest" -> <|"Blink" -> 0.888889, "Brow" -> 0.4, 
  "Control" -> 0.833333, "Smile" -> 0.444444|>, 
"NearestNeighbors" -> <|"Blink" -> 0.714286, "Brow" -> 0.769231, 
  "Control" -> 0.333333, "Smile" -> 0.571429|>|>}|>

Making unpractical as I cannot queue through multiple layers of the association table like:

combineTest["Duhe William", "NeuralNetwork"]

Any tips?

$\endgroup$

marked as duplicate by Kuba, J. M. is away Feb 26 '16 at 22:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Am I correct? red@{test1, test2} should work. $\endgroup$ – Kuba Feb 26 '16 at 22:45
  • $\begingroup$ You are certainly right, this is confirmed working. Thank you again $\endgroup$ – William John-Pierre Duhe Feb 26 '16 at 22:48
  • $\begingroup$ Does combineTest = Merge[{test1, test2}, Association] fail here? $\endgroup$ – Mr.Wizard Feb 26 '16 at 23:00
  • $\begingroup$ I will check in just a bit and let you know. $\endgroup$ – William John-Pierre Duhe Feb 26 '16 at 23:45
0
$\begingroup$

Per Kuba's suggestion it is shown that:

red = If[MatchQ[#, {__Association}], Merge[#, #0], Last[#]] &;

Gathered from: How to organically merge nested associations?

Works to solve this issue and produces:

red@{test1, test2}

<|"GlobalFace" -> <|"NeuralNetwork" -> <|"Blink" -> 0.666667, 
 "Brow" -> 0.461538, "Control" -> 0.780488, "Smile" -> 0.5|>, 
"LogisticRegression" -> <|"Blink" -> 0.680851, "Brow" -> 0.421053, 
 "Control" -> 0.829268, "Smile" -> 0.588235|>, 
"SupportVectorMachine" -> <|"Blink" -> 0.631579, 
 "Brow" -> 0.682927, "Control" -> 0.744186, "Smile" -> 0.578947|>,
"RandomForest" -> <|"Blink" -> 0.780488, "Brow" -> 0.8, 
 "Control" -> 0.829268, "Smile" -> 0.736842|>, 
"NearestNeighbors" -> <|"Blink" -> 0.697674, "Brow" -> 0.697674, 
 "Control" -> 0.780488, "Smile" -> 0.606061|>|>, 
"Madeline Perault" -> <|"NeuralNetwork" -> <|"Blink" -> 0.909091, 
 "Brow" -> 0.545455, "Control" -> 1., "Smile" -> 0.5|>, 
"LogisticRegression" -> <|"Blink" -> 0.8, "Brow" -> 0.545455, 
 "Control" -> 0.833333, "Smile" -> 0.571429|>, 
"SupportVectorMachine" -> <|"Blink" -> 1., "Brow" -> 0.769231, 
 "Control" -> 1., "Smile" -> 0.571429|>, 
"RandomForest" -> <|"Blink" -> 0.888889, "Brow" -> 0.4, 
 "Control" -> 0.833333, "Smile" -> 0.444444|>, 
"NearestNeighbors" -> <|"Blink" -> 0.714286, "Brow" -> 0.769231, 
 "Control" -> 0.333333, "Smile" -> 0.571429|>|>|>
$\endgroup$

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