# Select sublists with second element nearest to zero

I've got some big lists from which i need some data (one of the big lists). To be specific, I need to divide this biglists in sublists of the same length (10000), and form a new list with the mean of the fourth column of each sublist, and the element of the third column whose correspongin pair of the second column is closest to zero.

That sounds a little weird, but since i can't explain it any other way, here's an example: Suppose i have a list which i divide into sublists that look like this

sublist={{1,2,3,4},{1,.2,17,4.02},{1,.22,14,4.16}}

Then what i need too get from each sublist is

newlist={4.06,17}

(Mean[{4.02, 4, 4.16}]=4.06)

EDIT With @David Carraher 's solution, plus @Mr.Wizard 's suggestion (and the use of ReadList):

SetDirectory@NotebookDirectory[];
timetotal := 60;srate := 10000;
T[R_] := R/.381 - (3.81*10^-3)^-1;
rules := {
{x_, y_, z_, w_} -> {x, y, z, T[w/.0016]}
};
Skip[strm, Record, 3, NullRecords -> False];
meas = ReadList[strm, Real, RecordLists -> True] /. rules;
lst = Partition[meas, srate];
Table[
{Mean@lst[[i]][[All, 4]],
lst[[i]][[First@Ordering[Abs@lst[[i]][[All, 2]], 1], 3]]},
{i, timetotal}];


This code executes in 8.0484603 seconds, which is a bit more than 10 seconds faster than before. Thanks people!

{Mean@sublist[[All, 4]], sublist[[Ordering[Abs@sublist[[All, 2]]][], 3]]}


{4.06, 17}

Explanation

Mean@sublist[[All, 4]] provides the mean of the fourth column.

4.06

Ordering[Abs@sublist[[All, 2]]][] returns the row with the closest value to zero, i.e. with the smallest absolute value.

2

sublist[[Ordering[%, 3]] returns the element in row 2, col 3.

17

• I would like to mention for the record that even though David answered more quickly and has a more efficient solution, that I put {mean,val} in the right order :-) +1 from me David, good job! Sep 1, 2013 at 0:24
• Good catch, tjm. Sep 1, 2013 at 0:49
• Love it! As soon as i can re-write my code i'll upload as an edit here. Than you very much!
– Juan
Sep 1, 2013 at 2:02
• This is exactly the same way I'd do it, except I'd use the second argument of Ordering so as to not sort the entire second column. {Mean @ dat[[All, 4]], dat[[First @ Ordering[Abs @ dat[[All, 2]], 1], 3]]} @Juan for you too. Sep 1, 2013 at 2:20
• Hmm. I wasn't aware that Ordering had a second argument. Sep 1, 2013 at 4:13
{Mean@#[[All, 4]], #[[1, 3]]} &@SortBy[sublist, Abs@#[] &]


If I'm interpreting your problem correctly:

Given:

sublist = {{1, 2, 3, 4}, {1, .2, 17, 4.02}, {1, .22, 14, 4.16}};


Then:

(*Find the mean of all of the elements in column four of each sublist*)
theMean = Mean[sublist[[#, 4]] & /@ Range[Length[sublist]]];

(*Get all of the elements in column 2 of each sublist*)
secElements = sublist[[#, 2]] & /@ Range[Length[sublist]];

(*Find which sublist has the smallest column 2 element*)
smallestSubList = Flatten[Position[secElements, Min[secElements]]];