# Applying different functions to different columns of a Dataset

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

SeedRandom
dataSet = Dataset[AssociationThread[{"a", "b", "c", "d"} -> #] & /@ RandomReal[4, {10, 4}]] 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}

• 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. Oct 9 '14 at 20:09
• @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. Oct 9 '14 at 20:17
• @RunnyKine Is it possible to localize this bug? Oct 9 '14 at 20:35
• @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. Oct 9 '14 at 21:14

The following should work and can be easily extended:

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

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

(* RunnyKine *)

dataSet[Transpose /* ({Total@#[], Mean@#[],
Max@#[]} &), {#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} *)

• Hm... Do you know why your and OP's results differ from my and @alancalvitti results? Oct 9 '14 at 19:43
• 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. Oct 9 '14 at 20:01
• @ybeltukov. You're right. I just re-computed it after restarting my kernel and got same value as you. Hmm. Oct 9 '14 at 20:07
dataSet[{ Query[Total, "b"], Query[Mean, "a"], Query[Max, "d"]}] dataSet[<|"total b" -> Query[Total, "b"],
"mean a" -> Query[Mean, "a"], "max d" -> Query[Max, "d"]|>] From the documentation

dataSet[Total, "b"]


22.0943

Therefore

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}