MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37252] Re: grouping and averaging {x,y} pairs of data
  • From: "Carl K. Woll" <carlw at u.washington.edu>
  • Date: Fri, 18 Oct 2002 05:17:54 -0400 (EDT)
  • Organization: University of Washington
  • References: <aokc56$ah2$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi David,

One typical idea for your application is to sort the data and then use
Split. For example,

({#1[[1,1]], Plus @@ #1[[All,2]]/Length[#1]} & ) /@ Split[Sort[data],
#1[[1]] == #2[[1]] & ];

The only difference between the above function and your desired result is
that when there is no data for a particular integer, the average for that
integer does not appear in the answer.

Carl Woll
Physics Dept
U of Washington

"David E. Burmaster" <deb at alceon.com> wrote in message
news:aokc56$ah2$1 at smc.vnet.net...
> 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: List processing
  • Next by Date: Re: List processing (2)
  • Previous by thread: Re: grouping and averaging {x,y} pairs of data
  • Next by thread: RE: grouping and averaging {x,y} pairs of data