# Combining data sets by adding y values [duplicate]

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.

## marked as duplicate by Kuba♦, Karsten 7., kale, m_goldberg, MarcoBJul 8 '15 at 15:08

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


or

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


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

• To have the output sorted by the x values, one can add // SortBy[First] to the end of the code. – Karsten 7. Jul 8 '15 at 13:59
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}