Group Abstract Group Abstract

Message Boards Message Boards

Adjust values in a dataset relative to their maximum

Posted 4 days ago

I have the following dataset:

ds = Dataset@{
   <|"x" -> 1, "y" -> 45|>,
   <|"x" -> 2, "y" -> 19|>,
   <|"x" -> 3, "y" -> 20|>,
   <|"x" -> 4, "y" -> 47|>,
   <|"x" -> 5, "y" -> 30|>
};

How can I transform the "y" values so that each y_i is replaced by y_i - Max[#y], where Max[#y] is the maximum of the "y" column.

I would like to verify whether this means that I need to store in some variable the value of Max[#x], or this rather can be done in one step with a pure function.

POSTED BY: Ehud Behar
3 Replies
Posted 4 days ago

I think you'll need to capture the max separately:

With[
 {max = ds[Max, "y"]},
 ds[All, {"y" -> (#/max &)}]]
POSTED BY: Eric Rimbey

Here is one way (but probably not the most elegant one):

ds[All, <|"x" -> #x, "y" -> #y - ds[Max, "y"]|> &]

My suspicion is that this not very effective, because this way ds[Max, "y"] seems to be evaluated again and again, as this does suggest so (using nn as a counter):

nn = 0;
ds[All, <|"x" -> #x, "y" -> #y - (nn++; ds[Max, "y"])|> &];
nn
(*   Out:  5   *)
POSTED BY: Henrik Schachner
Posted 3 days ago

In cases like this there is a choice between code elegance or computation efficiency. To paraphrase Hamlet: To save or not to save? There is no fixed answer.

POSTED BY: Hans Milton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard