1
$\begingroup$

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.

$\endgroup$

marked as duplicate by Kuba, Karsten 7., kale, m_goldberg, MarcoB Jul 8 '15 at 15:08

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.

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

or

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

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

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

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