Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [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


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