I am trying to plot a list of tuples where each “x” coordinate has multiple corresponding “y” values, sort of like {{1, 0.5}, {1, 0.7}, {2, 0.8}, {2, 1.0}…}
. I want to create a new list where all the “y” coordinates corresponding to a particular “x” coordinate have been averaged over. So, the previous list would be transformed into {{1, 0.6}, {2, 0.9}…}
.
What is the cleanest way I can achieve this? I don’t want to write a for-loop or something like that for such a seemingly-trivial thing.
SortBy[yourlist,First]
will sort by x values. ThenSplitBy[thatresult,First]
will break those into groups of equal x values. ThenMap[Mean,thatnewresult]
will give you a list of{x,yavg}
for each of yourx
. Try that, step by step, and see the result. OrListPlot[Map[Mean,SplitBy[SortBy[yourlist,First],First]]]
Study that until you think you understand my thinking. $\endgroup$