I have data in form of:
data={{1,1,3},{1,1,6},{2,1,7},{2,2,4},{2,1,1}};
I want to match the entries with the same first two elements and add the third element in matching entries, so the result would be:
result={{1,1,9},{2,1,8},{2,2,4}}
For now, this is accomplished by using matching rules, like this:
data//.{h___, {x_, y_, z_}, b___, {x_, y_, f_}, t___} :> {h, {x, y, z + f}, b, t};
Needless to say, the procedure gets painfully slow with lists longer than a few hundred entries. I'm trying to come up with faster alternatives, I already looked at the examples with 1 matching element, but I can't quite expand it to two elements as I'm still fairly unfamiliar with all the operators and procedures.
Regards, blairo
KeyValueMap[Append, GroupBy[data, Take[#, 2] &, Total[#[[All, -1]]] &]]? – J. M.'s missing motivation May 09 '16 at 11:52#example:KeyValueMap[Append, GroupBy[data, Most, Last@*Total]]– Kuba May 09 '16 at 12:13Last@*Totalwastes a few additions. – J. M.'s missing motivation May 09 '16 at 12:56Tr@*Last@*Transpose:) – Kuba May 09 '16 at 13:00