1

This seems like the sort of thing where Mathematica can give a very simple solution.

I have a list of points {x,y}:

{{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 5}}

From this list, I want to generate a new list of pairs {x,ymean}, where ymean is the average of all y's in the original list which were paired to the same value of x:

{{1, 3}, {2, 7/2}, {3, 5}}

What's the simplest, most elegant way to accomplish this in Mathematica?

a06e
  • 11,327
  • 4
  • 48
  • 108

2 Answers2

6

Assuming you have your 2D list in a variable named lis, then the following should work:

Mean /@ GatherBy[lis, First]
RunnyKine
  • 33,088
  • 3
  • 109
  • 176
0
Replace[GatherBy[data, First], d_List :> {First@First@d, Mean[d[[All, 2]]]}, {1}]

where data is the original list of points.

a06e
  • 11,327
  • 4
  • 48
  • 108