Message Boards Message Boards

0
|
5092 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Lookup across datasets

Posted 9 years ago

I would like to output the value of "out" that corresponds to a certain value of "in" in the following association:

a = {<|"in" -> 0, "out" -> 1|>, <|"in" -> 1, "out" -> 3|>, <|
   "in" -> 2, "out" -> 4|>}

So for example, I would like to lookup the minimum value of "in" (which is 0 in the above) and get out the associated value of "out" (which is 1 in the above). I can find the minimum value of "in" with:

Min[<|"in" -> #["in"]|> & /@ a]

but, since Lookup refers to the value against the key, not another value in the table, I can't work out how to output the corresponding "out" value.

POSTED BY: Martin Currie
3 Replies

Thank you, and now that I understand how to do it & have played around with the code, I can also confirm that combining this into a single line, that avoids the need for a new parameter is possible:

Dataset[a][Select[#in == Dataset[a][Min, "in"] &], "out"]

However, both ways the output is actually {1} rather than 1

As a quick fudge to get a value out rather than a single membered list I am using

Min[Dataset[a][Select[#in == Dataset[a][Min, "in"] &], "out"]]
POSTED BY: Martin Currie
Posted 9 years ago

I just notice you wanted the out value for the minimum value of "in" --> you hav eto change the last line above to:

b[Select[#in == b[Min, "in"] &], "out"]
POSTED BY: Michael Helmle
Posted 9 years ago

create a dataset from a

b = Dataset[a]

Build a query for the value of "in"

b[Select[#in == 0 &], "out"]
POSTED BY: Michael Helmle
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract