-1
$\begingroup$

Here's the problem: I take a count of a count, so that I end up with a list in this format:

Table[Counts@Counts@Join[dKm[[i]], Pick[data1, ccKMedoid, i]], {i, 5}]

Result:

{<|1 -> 76, 2 -> 2|>, <|2 -> 23, 1 -> 32|>, <|2 -> 48, 1 -> 4|>, <|2 -> 50, 1 -> 2|>, <|2 -> 50, 1 -> 42|>}

Now what I would like to do is add each element of this list, so I could get a final total of the counts of 2s and 1s. At first I thought what I should do was sort the list of rules by numeric order, so that it was in the format

{<|1 -> 76, 2 -> 2|>, <|1 -> 32, 2 -> 23|>, <|1 -> 4, 2 -> 48|>, <|1 -> 2, 2 -> 50|>, <|1 -> 42, 2 -> 50|>}

and tried implementing the code here: Sort List of Rules in Numeric Order to do so, by doing something like:

SortBy[Table[Counts@Counts@Join[dKmn[[i]], Pick[data1, ccKMean, i]], {i, 5}], First[#]]

but that didn't work. Any advice on how to organize the list so I can use Total[] on it, or a smarter way to go about adding up the counts, would be much appreciated.

$\endgroup$
3
$\begingroup$
assoc = {<|1 -> 76, 2 -> 2|>, <|2 -> 23, 1 -> 32|>, <|2 -> 48, 
    1 -> 4|>, <|2 -> 50, 1 -> 2|>, <|2 -> 50, 1 -> 42|>};

assoc // Merge[Total]

(* <|1 -> 156, 2 -> 173|> *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Wow! That's exactly what I needed! Thanks a lot. $\endgroup$ – Jomy Blue Apr 17 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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