I have a list of (integer) 2d points, and I want to calculate the mean at each point in the first coordinate. So if my data is:
data = {{8,0},{7,0},{7,0},{6,0},{6,0},{6,0},{5,0},{5,0},{5,0},{5,0},{4,1},{4,0},{4,0},{4,0},{4,0},{3,1},{3,1},{3,0},{3,0},{3,0},{3,1},{2,1},{2,1},{2,1},{2,0},{2,0},{2,1},{2,1},{1,1},{1,1},{1,1},{1,1},{1,0},{1,1},{1,1},{1,1}}; then I want my output to be:
{{1, 7/8}, {2, 5/7}, {3, 1/2}, {4, 1/5}, {5, 0}, {6, 0}, {7, 0}, {8,0}} My initial data will be unsorted, but that is quick to fix. I can do this with the following awkward looking construction:
meanByFirstComponent[data_] := Module[{dataUnion, reorderedData}, dataUnion = (Union@data[[All, 1]]); reorderedData = Table[Select[data, #[[1]] == ii &], {ii, dataUnion}]; Mean /@ reorderedData] but this is very slow for a large list:
data = With[{n = 400}, Transpose[{RandomInteger[n, n^2], RandomReal[{0, 1}, n^2]}]]; AbsoluteTiming[meanByFirstComponent[data];] {53.963087, Null} Essentially I want to partition a sorted list that looks like:
{{1,1},{1,2},{2,1},{2,2},{3,1}} into
{{{1,1},{1,2}},{{2,1},{2,2}},{{3,1}}} and I can't work out how to do this efficiently.