RE: grouping and averaging {x,y} pairs of data

• To: mathgroup at smc.vnet.net
• Subject: [mg37222] RE: grouping and averaging {x,y} pairs of data
• From: "DrBob" <drbob at bigfoot.com>
• Date: Thu, 17 Oct 2002 00:08:51 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```You'll get more efficient methods from others, but I think the following
is instructive:

data = {{3, 1}, {4, 3}, {3, 2}, {1, 10}, {4, 2}, {1, 6}, {5, 2}, {2, 5},
{7, 1}};
ClearAll[total, count]
total[x_] := 0
count[x_] := 0
{total[#[[1]]] += #[[2]], count[#[[1]]]++} & /@ data;
?total
?count
{#, total[#]/count[#]} & /@ Union[data[[All, 1]]]

DrBob

-----Original Message-----
From: David E. Burmaster [mailto:deb at alceon.com]
To: mathgroup at smc.vnet.net
Subject: [mg37222]  grouping and averaging {x,y} pairs of data

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?/

many thanks
dave

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
David E. Burmaster, Ph.D.
Alceon Corporation
POBox 382069                 (new Box number effective 1 Sep 2001)
Harvard Square Station
Cambridge, MA 02238-2069     (new ZIP code effective 1 Sep 2001)

Voice	617-864-4300

Web	http://www.Alceon.com
Email	deb at Alceon.com
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

```

• Prev by Date: Re: Boole Function
• Next by Date: Re: grouping and averaging {x,y} pairs of data
• Previous by thread: Re: grouping and averaging {x,y} pairs of data
• Next by thread: Re: grouping and averaging {x,y} pairs of data