1
$\begingroup$

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?

$\endgroup$

marked as duplicate by Kuba, MarcoB, user9660, m_goldberg, dr.blochwave Nov 12 '15 at 22:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
$\begingroup$

Let's say this is your data:

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

TableForm[data, TableHeadings -> Automatic]

Mathematica graphics

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]]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.