Joining lists with same elements
I suggest you use associations, not lists of pairs.
{asc1, asc2, asc3} = AssociationThread @@ Transpose[#] & /@ {data1, data2, data3};
asc1 + asc2 + asc3
(* <|1 -> -1, 2 -> 1.3, 3 -> -1.997, 4 -> 6.2, 5 -> 0.2|> *)
Look up KeyUnion
and KeyIntersection
for dealing with the cases when the set of keys are not exactly the same.
You may also use TimeSeries
, though I am personally not very experienced with this construct, so I will only show the simplest example:
{ts1, ts2, ts3} = TimeSeries /@ {data1, data2, data3};
Now we can do
tsSum = ts1 + ts2 + ts3
Convert back to a list:
Normal[tsSum]
(* {{1, -1}, {2, 1.3}, {3, -1.997}, {4, 6.2}, {5, 0.2}} *)
Most operations that one might want to do on time series data, such as ListPlot
or LinearModelFit
, will work without needing to convert the TimeSeries
back to a list.
Values @ GroupBy[Join[data1, data2, data3], First, {#[[1, 1]], Total[#[[All, 2]]]} &]
{{1, -1}, {2, 1.3}, {3, -1.997}, {4, 6.2}, {5, 0.2}}
Also (thanks WReach):
KeyValueMap[List] @ GroupBy[Join[data1, data2, data3], First -> Last,Total]
{{1, -1}, {2, 1.3}, {3, -1.997}, {4, 6.2}, {5, 0.2}}
More generally, if the input lists are ragged lists:
KeyValueMap[List] @ GroupBy[Join[data1, data2, data3], First -> Rest, First@*Total]
{#[[1, 1]], #[[All, 2]] // Total} & /@ GatherBy[Join[data1, data2, data3], First]
{{1, -1}, {2, 1.3}, {3, -1.997}, {4, 6.2}, {5, 0.2}}