# How to select data using conditions from associations?

Consider the following dataset:

data = Table[{RandomVariate[NormalDistribution[]],
RandomVariate[NormalDistribution[]],
RandomVariate[NormalDistribution[]]}, {i, 1, 10^3, 1}];


I would like to impose different conditions on it using associations:

condition :=
Association[{"First" -> 1 < #[[1]] < 2,
"Second" -> ArcTan[#[[1]]] > 0.5,
"Third" -> Exp[-(#[[1]]^2/2)] < 0.05}]
selectedData[selection_] := Select[data, condition[selection] &]


However, something went wrong: while

Select[data,1 < #[[1]] < 2&][[1]]


works, the desired code fails:

selectedData["First"][[1]]


Part::partw: Part 1 of {} does not exist.

It seems that the condition inside the association is not recognized as a normal condition:

condition["Second"]


True

Could you please tell me how to fix selectedData?

Try these modifications:

ClearAll[condition, selectedData]

condition =
Association[{
"First" -> (1 < #[[1]] < 2 &),
"Second" -> (ArcTan[#[[1]]] > 0.5 &),
"Third" -> (Exp[-(#[[1]]^2/2)] < 0.05 &)
}]

selectedData[selection_] := Select[data, condition[selection]]

selectedData["First"]

(* Out: {1.47076, 0.65529, 0.927465} *)


In short, associate fully-constructed pure functions to the labels, so the association query returns a whole working pure function that can be used inside Select.

As a note, the parentheses in the association are needed for precedence reasons.