Suppose I have different functions: Total, Mean and Max and the following Dataset

dataSet = Dataset[AssociationThread[{"a", "b", "c", "d"} -> #] & /@ RandomReal[4, {10, 4}]]

Mathematica graphics

How can I do the following: apply Total to column "b", Mean to column "a" and Max to column "d", in that order without leaving the Dataset i.e. no use of Normal? So the result should be

{22.09, 2.383, 3.765}

  • $\begingroup$ Please confirm that your answer is what you expect. Maybe try restarting your session. I initially got same answer as you, but that's obviously wrong. $\endgroup$
    – RunnyKine
    Commented Oct 9, 2014 at 20:09
  • $\begingroup$ @RunnyKine. You're right, restarting Mathematica gave me a different result. I had used a method that was convoluted that gave me that answer. It seemed to work when I tested it, so I don't know what happened there. I will update it now. Am I right to assume there are hidden bugs with Dataset as it currently stands. $\endgroup$
    – smayhem
    Commented Oct 9, 2014 at 20:17
  • $\begingroup$ @RunnyKine Is it possible to localize this bug? $\endgroup$
    – ybeltukov
    Commented Oct 9, 2014 at 20:35
  • $\begingroup$ @ybeltukov. I don't know, but this is not the first time I've experienced this sort of weird behavior from using Dataset. Every time, restarting the kernel solves the problem. $\endgroup$
    – RunnyKine
    Commented Oct 9, 2014 at 21:14

3 Answers 3


The following should work and can be easily extended:

dataSet[Transpose /* ({Total@#[[1]], Mean@#[[2]], Max@#[[3]]} &), {#b, #a, #d} &]

Mathematica graphics


dataSet = Dataset[AssociationThread[{"a", "b", "c", "d"} -> #] & /@ RandomReal[4, {1000000, 4}]];

(* RunnyKine *)

dataSet[Transpose /* ({Total@#[[1]], Mean@#[[2]], 
      Max@#[[3]]} &), {#b, #a, #d} &]; // AbsoluteTiming

(* {0.968942, Null} *)

(* ybeltukov faster *)

dataSet @@@ {{Total, #b &}, {Mean, #a &}, {Max, #d &}}; // AbsoluteTiming

   (* {1.218837, Null} *)

(* alancalvitti *)

dataSet[{Query[Total, "b"], Query[Mean, "a"], Query[Max, "d"]}]; // AbsoluteTiming

(* {11.037016, Null} *)
  • $\begingroup$ Hm... Do you know why your and OP's results differ from my and @alancalvitti results? $\endgroup$
    – ybeltukov
    Commented Oct 9, 2014 at 19:43
  • $\begingroup$ I have the same table as OP! But summation over the column "b" give me 22.09 even with manual summation. I use version 10.0.1 under Linux. $\endgroup$
    – ybeltukov
    Commented Oct 9, 2014 at 20:01
  • $\begingroup$ @ybeltukov. You're right. I just re-computed it after restarting my kernel and got same value as you. Hmm. $\endgroup$
    – RunnyKine
    Commented Oct 9, 2014 at 20:07
dataSet[{ Query[Total, "b"], Query[Mean, "a"], Query[Max, "d"]}]

enter image description here

Can be adapted to association:

dataSet[<|"total b" -> Query[Total, "b"], 
  "mean a" -> Query[Mean, "a"], "max d" -> Query[Max, "d"]|>]

enter image description here


From the documentation

dataSet[Total, "b"]



dataSet @@@ {{Total, "b"}, {Mean, "a"}, {Max, "d"}}

{22.0943, 2.38258, 3.76476}

Faster version

dataSet @@@ {{Total, #b &}, {Mean, #a &}, {Max, #d &}}

{22.0943, 2.38258, 3.76476}


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.