I would like to plot a histogram for multiple datasets in a dataset query, but can't quite seem to make it work in operator notation:

titanic = ExampleData[{"Dataset", "Titanic"}];
titanic = Dataset@Flatten@Table[
     Map[Append[#, "score" -> RandomReal[100]] &, Normal@titanic], 

(*this ugly version works:*)
Histogram @ Transpose[List @@@ titanic[Slice[All, {"age", "score"}]] // Normal]

(*but the operator forms version does not:*)
titanic[Histogram, Slice[All, {"age", "score"}]]
titanic[Slice[All, {"age", "score"}] /* Histogram]

enter image description here

How to do this with just operator forms?

  • $\begingroup$ What is the Slice function? $\endgroup$
    – Edmund
    Sep 12 '18 at 2:24
  • $\begingroup$ It's in GeneralUtilities $\endgroup$
    – M.R.
    Sep 12 '18 at 2:31
  • $\begingroup$ Also note that Transpose@ titanic[Transpose /* Query[<|#, "score" -> RandomReal[{0, 100}, 1309]|> &]] is a more efficient addition of "score". $\endgroup$
    – Edmund
    Sep 12 '18 at 2:35
  • 1
    $\begingroup$ closely related: How to pass options to ListPlot when using a DataSet? $\endgroup$
    – kglr
    Sep 12 '18 at 2:36

You may use Composition with Transpose.

titanic[Histogram@*Transpose, {"age", "score"}]

Mathematica graphics

Hope this helps.


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.