# Problem averaging multiple series of data [duplicate]

Recently I'm having trouble figuring out a certain problem in Mathematica. I have a table of data in the following arrangement:

{series no., x, y}

Now, each series of data has exactly the same x values, with different y. What I am trying to achieve is averaging the y values for all series of data for each x, so that the final table looks like this:

{x, average y}

Do you have any ideas on how to solve it efficiently?

## marked as duplicate by Kuba♦, MarcoB, user9660, m_goldberg, dr.blochwaveNov 12 '15 at 22:40

• The difference is you have to use Mean instead of Total because the linked topic is about sum. – Kuba Nov 12 '15 at 13:25
• Greetings! To make the most of Mma.SE please take the tour now. Help us to help you, write an excellent question. Edit if improvable, show due diligence, give brief context, include minimum working examples of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. – rhermans Nov 12 '15 at 13:48
• – Kuba Nov 12 '15 at 14:02

Let's say this is your data:

data = Transpose@{RandomInteger[10, 20], RandomInteger[5, 20], RandomReal[1, 20]};



Use Part to get only x and y part and GatherBy to gather by x

GatherBy[Part[data, All, {2, 3}], First]

 {
{{0, 0.554355}, {0, 0.669234}, {0, 0.673409}}
, {{3, 0.107376}, {3, 0.535935}, {3, 0.836943}, {3, 0.479782}, {3, 0.176511}, {3, 0.966172}, {3, 0.896234}, {3, 0.784782}}
, {{2, 0.319966}, {2, 0.0572314}, {2, 0.450977}}
, {{5, 0.980007}, {5, 0.49463}}
, {{1, 0.0754787}, {1, 0.307566}, {1, 0.77322}}
, {{4, 0.656813}}
}


Now you only need to Map the Mean over the list

Mean /@ GatherBy[Part[data, All, {2, 3}], First]

{
{0, 0.632333}
, {3, 0.597967}
, {2, 0.276058}
, {5, 0.737318}
, {1, 0.385422}
, {4, 0.656813}
}


You can create a function that does that for later use

avgx[data_] := Map[Mean, GatherBy[Part[data, All, {2, 3}], First]]