# How-to select an entry from a list of pairs that meets a condition depending the 2nd element of each pair

Processed data structure

data = Transpose[{RandomInteger[{1, 20}, 100], RandomReal[{10^-8, 10^-1}, 100]}]


Condition description

Firstly, the decision must be based on the sub-lists 2nd element. Please see example below:

{{15, 0.0690906}, {18, 0.095235}, {17, 0.0282053}, {9, 0.00283472}, ...}

Based on the above example, I need to extract a value nearest to x, where x signifies some specific treshold. Examples can be:

{10^-1, 10^-2, 10^-3} etc.

I have tried to build up a solution using a set of configurations involving Cases, Select and Nearest. However, I did not succeed.

I have tried using a formulation similar to below:

(*Simplified processed data*)
Select[RandomInteger[{1, 20}, {5, 2}], #[[2]] > 5 &]


Based on the generated data, the above would produce desired output. Example:

{{19, 18}, {9, 11}}

However, my problem involves the condition to be as described above. Based on raw data structure (transposition of column 1 && 3), I have applied following formulation to see what is the actual nearest result:

Input:

Nearest[dataF[[1]][[All, 2]], 10^-4]


In the above dataF is processed raw data (transposition of column 1 && 3). Additionally, dataF is composed of many similar sets of data as the raw data structure made available to download above; hence [[1]] notation used to point towards a specific dataset.

Output:

{0.000101238}

Given the above has output a value which could be used to find a sub-list containing this value, I used following:

Input:

Select[dataF[[1]], #[[2]] == Nearest[dataF[[1]][[All, 2]], 10^-4] &]


Output:

{}

I would appreciate if somebody could point me in the right direction and, perhaps, explain why the above code did not produce any result.

SeedRandom[42];
With[{n = 100},
data = Transpose[{RandomInteger[{1, 20}, n], RandomReal[{10^-8, 10^-1}, n]}]]
With[{threshold = 1*^-4, yvals = data[[All, 2]]},
First @ Extract[data, Position[yvals, First @ Nearest[yvals, threshold]]]]


{16, 0.000661563}

SeedRandom[42]; With[{n = 100},
data = Transpose[{RandomInteger[{1, 20}, n],
RandomReal[{10^-8, 10^-1}, n]}]] ;

Pick[data, Chop[data[[;; , 2]] - 1*^-4, 0.001], 0]
(*{{16, 0.000661563}}*)

dataF = Catenate@Import["excel.xlsx"];

dataF // Dimensions


{800, 3}

The second column contains the same value:

dataF[[All, 2]] // Union


{7.7875*10^-6}

sel = First@Nearest[dataF[[All, 3]], 10^-4]


0.000101238

Flatten@Select[dataF, #[[3]] == sel &]


{0.196152, 7.7875*10^-6, 0.000101238}

Function[{x},
Flatten@Select[dataF, #[[3]] ==
First@Nearest[dataF[[All, 3]], x] &]][#] & /@
{10^-4, 10^-3, 10^-2, 10^-1}


{{0.196152, 7.7875*10^-6, 0.000101238}, {0.156825, 7.7875*10^-6, 0.0009968}, {0.0418247, 7.7875*10^-6, 0.00623}, {0.0418247, 7.7875*10^-6, 0.00623}}

etc.