Given the data from the question:
data = {<|Year->2014, ID->1, amt1->10, amt2->20|>,
<|Year->2015, ID->1, amt1->20, amt2->25|>,
<|Year->2014, ID->2, amt1->11, amt2->21|>};
We can introduce the key tot
which is the sum of amt1
and amt2
like this:
<| #, tot -> #[amt1] + #[amt2] |> & /@ data
(*
{<| Year->2014, ID->1, amt1->10, amt2->20, tot->30 |>,
<| Year->2015, ID->1, amt1->20, amt2->25, tot->45 |>,
<| Year->2014, ID->2, amt1->11, amt2->21, tot->32 |>}
*)
Should we wish to drop the input keys amt1
and amt2
:
<| KeyDrop[#, {amt1, amt2}], tot -> #[amt1] + #[amt2] |> & /@ data
(*
{<| Year->2014, ID->1, tot->200 |>,
<| Year->2015, ID->1, tot->500 |>,
<| Year->2014, ID->2, tot->231 |>}
*)
In some situations, it might just be easiest to build the result associations from scratch:
<| Year -> #[Year], ID -> #[ID], tot -> #[amt1] + #[amt2] |> & /@ data
(*
{<| Year->2014, ID->1, tot->30 |>,
<| Year->2015, ID->1, tot->45 |>,
<| Year->2014, ID->2, tot->32 |>}
*)