Suppose I have a dataset with a header row.

ds = 
  Dataset[AssociationThread[{"x", "y", "z"}, #] & /@ RandomReal[{0, 1}, {10, 3}]];

I want to construct the following dataset of means and variances. Note the header row and the stub column.)

Dataset[<|"mean" -> Normal@ds[Mean], "var" -> Normal@ds[Variance]|>]

How can I construct that with a single query on ds (an no use of Normal)?

enter image description here


The query ds[<|"mean" -> Mean[#], "var" -> Variance[#]|> &] should give you the dataset you need, though the output doesn't format nicely for some reason. The formatting can be fixed by throwing an extra Dataset at it:

Dataset @ ds[<|"mean" -> Mean[#], "var" -> Variance[#]|> &]
  • $\begingroup$ +1 slightly shorter Dataset @ ds[<|"mean" -> Mean, "var" -> Variance|>] $\endgroup$ – WReach Feb 4 '18 at 4:05
  • $\begingroup$ @WReach So there is no query that can produce what I want directly? I ctually started with this query, did not like the result, but did not consider applying Dataset to get what I want, and I confess that I do not understand why this changes anything. (I.e., the underlying association structure seems to be identical, but for some reason the type system information is different and forces the different display. What is the underlying principle?) $\endgroup$ – Alan Feb 4 '18 at 16:32
  • $\begingroup$ I initially tried the similar query suggested by @WReach, but I did not guess that "throwing an extra Dataset at it" could help. Why does this matter? (I.e., what is the underlying principle?) $\endgroup$ – Alan Feb 4 '18 at 16:35
  • $\begingroup$ As you note, the different appearances are due to different types. Without the extra Dataset, the final type is determined by type inferencing. With it, the type is determined by type deduction. Both use heuristics but, empirically, type deduction tends to yield better results. These issues are discussed at more length in (143551) and (87479). $\endgroup$ – WReach Feb 4 '18 at 18:15

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.