8
$\begingroup$

How can I go from

as =  {
    <|A->{1,11},B->{1,12},C->{1,13}|>,
    <|A->{2,21},B->{2,22},C->{2,23}|>,
    <|A->{3,31},B->{3,32},C->{3,33}|>
}

to

<|
     "A" -> {{1, 11}, {2, 21}, {3, 31}}, 
     "B" -> {{1, 12}, {2, 22}, {3, 32}}, 
     "C" -> {{1, 13}, {2, 23}, {3, 33}}
|>

I think I should use some variation of GroupBy[as,condition] but I can't really figure out what condition should be.

$\endgroup$
1
  • $\begingroup$ No need for the version-10 tag; that is implied by the use of Association. $\endgroup$
    – Mr.Wizard
    Commented Sep 29, 2014 at 16:24

3 Answers 3

15
$\begingroup$

This operation is performed by Merge:

Merge[as, Identity]
<|A -> {{1, 11}, {2, 21}, {3, 31}},
  B -> {{1, 12}, {2, 22}, {3, 32}}, 
  C -> {{1, 13}, {2, 23}, {3, 33}}|>
$\endgroup$
9
$\begingroup$

Another approach:

AssociationThread[Keys[as][[1]], Transpose@Values[as]]
<|A -> {{1, 11}, {2, 21}, {3, 31}},
  B -> {{1, 12}, {2, 22}, {3, 32}}, 
  C -> {{1, 13}, {2, 23}, {3, 33}}|>
$\endgroup$
3
$\begingroup$
GroupBy[Catenate @ Normal @ as, First -> Last]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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