# How to select data from list of pairs given that one entry of each element is element of another list

I have data of the following form

data={{6, 23.3}, {6.1, 19.8}, {6.2, 25.1}, {6.3, 25.5}, {6.4, 24.5}, {6.5,
28.1}, {6.6, 24.9}, {6.7, 26.3}, {6.8, 27.3}, {6.9, 27.7}, {7,
33.9}, {7.1, 26.6}, {7.2, 31.3}, {7.3, 26.8}, {7.4, 31.5}, {7.5,
30.2}, {7.6, 29.8}, {7.7, 25.5}, {7.8, 31.5}, {7.9, 28.9}, {8,
28.1},...,}


This is a very long list of pairs, which are of the form $$(t,B)$$, time and magnetic field magnitude. Then I used a code to find relevant points that I need to use. Namely the peaks of the magnetic field. With the help of FindPeak I found that the Peak Magnetic moments are

peaks={{15, 31.5}, {99, 31.6}, {170, 45.7}, {224, 15.7}, {281, 15.6}, {319,
13.1}, {345, 10.5}, {349, 11.1}, {437, 8.4}}


I also found the corresponding time using peaktime = data[[First /@ peaks, 1]]

What I wish is to create a list of pairs, of the form $$(t_p,B_p)$$ such that $$t_p$$ and $$B_p$$ are the peak times and magnetic field magnitudes, respectively. For this I used a For loop, which is the following:

v = {};
For[i = 1, i <= Length[peaktime], i++,
u = Position[data[[All, 1]], peaktime[[i]]][[1]];
v = Union[v, u]]
peakscouuple = data[[v, All]];


This works, but it is too messy, Is there a cleaner way to do this?

• A minimum working example would be useful ... Commented Mar 16, 2020 at 19:04
• If I understand you correctly, you can just use data[[First/@peaks]] without the spec to take the first part like you used in peaktime=data[[...,1]] to get (t,B) pair corresponding to the peaks. Commented Mar 16, 2020 at 19:04
• You could also use Extract[data,Position[PeakDetect[Last/@data],1]]. Commented Mar 16, 2020 at 19:09

data = {{6, 23.3}, {6.1, 19.8}, {6.2, 25.1}, {6.3, 25.5}, {6.4, 24.5}, {6.5, 28.1},

data[[FindPeaks[data[[All, 2]]][[All, 1]]]]