This seems like it should be easy but I am trying to concisely enhance some nested associations with Mathematica objects (e.g., convert things to matrices, etc) but I'm having a bit of difficulty with nested items. In my case I want to go through an arbitrarily nested association and anywhere there is a key == 'somevalue' (correlation below) I want to replace its value with the result. KeyValueMap almost does the trick (see below). But it doesn't take a levelspec, I'm wondering if someone can show me how you get around this since some functions take levelspecs and some don't. Example code:
test = <| "name" -> "foo", "correlation" -> {{1, 2}, {3, 4}} ,
"descendants" -> <| "name" -> "bar", "correlation" -> {{12, 13}, {20, 22}}|> |>;
KeyValueMap[If[#1 == "correlation", #1 -> MatrixForm[#2], #1 -> #2] &, test]
Expected output would have both correlations formatted as matrices, but as you can guess the descendant one is missed with the code as is.