Skip to main content
added 61 characters in body
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751

Assuming vals has depth corresponding to dims length:

Module[{i = -2}
, Fold[
    Function[{val, dim}, Map[AssociationThread[dim -> #] &, val, {i--}]]
  , vals
  , Reverse @ dims
  ]
]
<|"New York City" -> <|"Terrace house" -> <|"Two bed" -> <|"2016" -> 
    0., "2017" -> 633.25|>, 
 "Three bed" -> <|"2016" -> 642.96, "2017" -> 677.34|>|>, 
   "Apartment" -> <|"Two bed" -> <|"2016" -> 591.56, 
   "2017" -> 632.45|>, 
 "Three bed" -> <|"2016" -> 634.9, "2017" -> 715.6|>|>|>|>
Module[{i = -2}
, Fold[
    Function[{val, dim}, Map[AssociationThread[dim -> #] &, val, {i--}]]
  , vals
  , Reverse @ dims
  ]
]
<|"New York City" -> <|"Terrace house" -> <|"Two bed" -> <|"2016" -> 
    0., "2017" -> 633.25|>, 
 "Three bed" -> <|"2016" -> 642.96, "2017" -> 677.34|>|>, 
   "Apartment" -> <|"Two bed" -> <|"2016" -> 591.56, 
   "2017" -> 632.45|>, 
 "Three bed" -> <|"2016" -> 634.9, "2017" -> 715.6|>|>|>|>

Assuming vals has depth corresponding to dims length:

Module[{i = -2}
, Fold[
    Function[{val, dim}, Map[AssociationThread[dim -> #] &, val, {i--}]]
  , vals
  , Reverse @ dims
  ]
]
<|"New York City" -> <|"Terrace house" -> <|"Two bed" -> <|"2016" -> 
    0., "2017" -> 633.25|>, 
 "Three bed" -> <|"2016" -> 642.96, "2017" -> 677.34|>|>, 
   "Apartment" -> <|"Two bed" -> <|"2016" -> 591.56, 
   "2017" -> 632.45|>, 
 "Three bed" -> <|"2016" -> 634.9, "2017" -> 715.6|>|>|>|>
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751

Module[{i = -2}
, Fold[
    Function[{val, dim}, Map[AssociationThread[dim -> #] &, val, {i--}]]
  , vals
  , Reverse @ dims
  ]
]
<|"New York City" -> <|"Terrace house" -> <|"Two bed" -> <|"2016" -> 
    0., "2017" -> 633.25|>, 
 "Three bed" -> <|"2016" -> 642.96, "2017" -> 677.34|>|>, 
   "Apartment" -> <|"Two bed" -> <|"2016" -> 591.56, 
   "2017" -> 632.45|>, 
 "Three bed" -> <|"2016" -> 634.9, "2017" -> 715.6|>|>|>|>