I learned how to query Dataset
for rows that contain a certain value, but now I need to query for a long series of And
in chain on the value of several columns. So in short I am trying to identify the row where x={x1, ..., xn} takes a certain value.
For a Dataset like this
ds = Dataset[{Association[{"x1" -> myX, "x2" -> myX, "x3" -> myX,
"x4" -> myX, "y" -> myY}],
Association[{"x1" -> myotherX, "x2" -> myotherX, "x3" -> myX,
"x4" -> myX, "y" -> myotherY}]}];
I can do
ds[Select[(#x1 == myotherX && #x2 == myotherX && #x3 == myX && #x4 ==
myX) &], "y"]
but the expression is long to type and less and less convenient as from 2 argument I go to a larger numbers.
Ideally I wanted to able to call something like
ReadRow[ds,<|"x1"->myX,"x2"->myX,"x3"->myX,"x4"->myX|>,"y"]
Any idea how to do this? something like this should be a standard way to deal with Dataset.
My best result so far is to "translate" the Association
into a check for the value I am looking for and put all the entered keys of the association
RuleToEquality[a_, b_] := Slot[a] == b
MgPyResults[
Select[(KeyValueMap[RuleToEquality, <|x1 -> myX|>][[1]]) &], "y"]
This, however, does not match the entries, despite the
InputForm[
Tr[KeyValueMap[RuleToEquality, <|x1 -> myX, x2 -> myX|>], And]]
gives the correct
Slot[x1] == myX && Slot[x2] == myX
ReadRow[ds,<|x1->myX,x2->myX,x3->myX,x4->myX|>,"y"]
theKeys
in your example are strings sox1
... should be wrapped in quotes. $\endgroup$ – alancalvitti Sep 3 '15 at 22:00