19
$\begingroup$

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

SeedRandom[0]
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}

$\endgroup$
  • $\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 Oct 9 '14 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 Oct 9 '14 at 20:17
  • $\begingroup$ @RunnyKine Is it possible to localize this bug? $\endgroup$ – ybeltukov Oct 9 '14 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 Oct 9 '14 at 21:14
13
$\begingroup$

The following should work and can be easily extended:

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

Mathematica graphics

Timings:

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} *)
$\endgroup$
  • $\begingroup$ Hm... Do you know why your and OP's results differ from my and @alancalvitti results? $\endgroup$ – ybeltukov Oct 9 '14 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 Oct 9 '14 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 Oct 9 '14 at 20:07
10
$\begingroup$
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

$\endgroup$
8
$\begingroup$

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}

$\endgroup$

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.