1

Suppose I have the following list of data

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

This is similar to my actual data in that each x value has several y values, potentially different numbers of y values, I don't necessarily have all of the x values, and the x values could be disordered. I want to combine this data set to look like {{1,10},{2,6},{5,6},{6,1}}, where I keep only the unique x values and have the sum of all the y values for that x value as the new y value. The sorting doesn't necessarily have to happen, but would be convenient if it did.

Is there a better way to do this than in a For loop going through each pair looking for the same x value and adding it to the relevant y value? I worry that this would be extremely inefficient for my actual data set which has several thousand points over a hundred or so x values.

Karsten7
  • 27,448
  • 5
  • 73
  • 134

2 Answers2

4
Normal@GroupBy[data, First -> Last, Total] /. Rule -> List

or

List @@@ Normal@GroupBy[data, First -> Last, Total]

{{1, 10}, {6, 1}, {2, 6}, {5, 6}}

Karsten7
  • 27,448
  • 5
  • 73
  • 134
3
list = {{1, 2}, {1, 5}, {6, 1}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {5, 
    4}, {5, 2}};
{#[[1, 1]], Total[#[[All, 2]]]} & /@ GatherBy[list, First]
{{1, 10}, {6, 1}, {2, 6}, {5, 6}}

You can then sort with:

SortBy[%, First]
{{1, 10}, {2, 6}, {5, 6}, {6, 1}
shrx
  • 7,807
  • 2
  • 22
  • 55