Re: grouping and averaging {x,y} pairs of data
- To: mathgroup at smc.vnet.net
- Subject: [mg37229] Re: grouping and averaging {x,y} pairs of data
- From: BobHanlon at aol.com
- Date: Thu, 17 Oct 2002 00:09:17 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 10/16/2002 4:30:28 PM, deb at alceon.com writes:
>I have a list of 17,000+ {x,y} pairs of data
>
> each x value is a positive integer from 1 to 100+
>
> each y value is a positive real number
>
>As a *short* example, let's consider:
>
> data = {{3,1},{4,3},{3,2},{1,10},{4,2},{1,6},{5,2},{2,5},{7,1}}
>
>I want to group the data by the x value and report the arithmetic average
>of the y values in each group.
>
>For the example, i want to report:
>
> output = {{1,8},{2,5},{3,1.5},{4,2.5},{5,2},{6,0},{7,1}}
>
>In this example, x=6 does not occur so i report the average y[6] = 0.
>
>Can anyone suggest a way to do this efficiently?/
>
The basic approach is
(Plus @@ #)/Length[#] & /@
Split[Sort[data], #1[[1]] == #2[[1]] &]
{{1, 8}, {2, 5}, {3, 3/2}, {4, 5/2}, {5, 2}, {7, 1}}
To fill in the gaps:
dataAvg[data_] :=
Module[{val = First /@ data, xData},
xData = Join[data, {#, 0} & /@
Complement[Range[Max[val]], val]];
(Plus @@ #)/Length[#] & /@
Split[Sort[xData], #1[[1]] == #2[[1]] &]];
dataAvg[data]
{{1, 8}, {2, 5}, {3, 3/2}, {4, 5/2}, {5, 2}, {6, 0}, {7, 1}}
Bob Hanlon