Re: grouping and averaging {x,y} pairs of data
- To: mathgroup at smc.vnet.net
- Subject: [mg37238] Re: grouping and averaging {x,y} pairs of data
- From: daiyanh at earthlink.net (Daitaro Hagihara)
- Date: Fri, 18 Oct 2002 05:16:55 -0400 (EDT)
- References: <aokc56$ah2$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <aokc56$ah2$1 at smc.vnet.net>, "David E. Burmaster"
<deb at alceon.com> wrote:
>Dear Fellows in MathGroup,
>
>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?/
Block[{data = {{3,1},{4,3},{3,2},{1,10},{4,2},{1,6},
{5,2},{2,5},{7,1}},
sd = Table[{0,0.},{10}]},
Off[Infinity::indet];
Off[General::dbyz];
(sd[[#[[1]],2]]++;sd[[#[[1]],1]]+=#[[2]])&/@data;
sd=MapIndexed[{#2[[1]],Divide@@#}&,sd];
On[Infinity::indet];
On[General::dbyz];
sd/.Indeterminate->0.]
-->
{{1, 8.}, {2, 5.}, {3, 1.5}, {4, 2.5}, {5, 2.}, {6, 0.},
{7, 1.}, {8, 0.}, {9, 0.}, {10, 0.}}
This makes 3 linear passes overall on the data. For
large data sets, that may be a problem.
DH