3

As a useful extension of this question:

Elegant operations on matrix rows and columns

I would like to ask:

What are for you elegant, short, useful or frequently used functions to use with associations or datasets?

I propose to distinguish between

  1. Simple associations
  2. Associations with named columns and rows
  3. Associations with named columns
  4. Datasets

Some examples for each of these four categories follow:

1. Simple associations

aset = <|"A" -> 10., "B" -> 20., "C" -> 30., "D" -> 40.|>;

MovingAverage was recently changed to also work with associations (undocumented):

MovingAverage[aset, 2]

<|"B" -> 15., "C" -> 25., "D" -> 35.|>

2. Associations of associations

mset =
  <|"10" -> <|"A" -> 10., "B" -> 20., "C" -> 30., "D" -> 40.|>,
   "20" -> <|"A" -> 50., "B" -> 60., "C" -> 70., "D" -> 80.|>,
   "30" -> <|"A" -> 90., "B" -> 100., "C" -> 110., "D" -> 120.|>,
   "40" -> <|"A" -> 130., "B" -> 140., "C" -> 150., "D" -> 160.|>,
   "50" -> <|"A" -> 170., "B" -> 180., "C" -> 190., "D" -> 200.|>|>;

Transpose

mset // Query[Transpose] // Dataset

enter image description here

Add column total

Query[<|#, "Total" -> Total[#]|> &] @ mset // Dataset

enter image description here

Extract

Query[{"10", "20"}, {"A", "B"}] @ mset // Dataset

enter image description here

3. Associations with named columns

JoinAcross

cset = Values @ mset;

join = {<|"A" -> 10., "E" -> "Red"|>, <|"A" -> 90., "E" -> "Blue"|>};

JoinAcross[cset, join, "A", "Left"] // SortBy["A"] // Dataset

enter image description here

4. Dataset

Display as BarChart

dset = Dataset[mset];

dset[BarChart[#, ChartLabels -> Automatic] &]

enter image description here

eldo
  • 67,911
  • 5
  • 60
  • 168

0 Answers0