Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

compress lists with mean for equal elements

1 view
Skip to first unread message

vJD

unread,
Jun 19, 2009, 8:45:47 PM6/19/09
to
I have a nested list of this form:

{{{1, 13}, 97.6493}, {{1, 13}, 97.9511}, {{1, 14}, 99.3002}, {{1, 14},
99.4602}, {{1, 14}, 99.6193}, {{1, 15}, 100.513}, {{1, 15}, 101.149},
{{1, 15}, 101.483}, {{1, 15}, 101.494}, {{1, 16}, 101.51}, {{1, 16},
101.895}}

I want to shrink the list in the way that I calculate the mean over
the second part for all elements where the first part is equal. so
e.g. the first element of the new list will be {{1,13}, 97.8002},
because they both have {1,13} and the mean of 97.6493 and 97.9511 is
97.8002.

The problem is that there is no fixed number for the equal elements
(so e.g. two elements with {1,13}, three elements with {1,14} and so
on). The number is random.

Can anybody please provide me with a statement or hint how to
accomplish that task.

Thank you very much

Regards,
Holger

Ray Koopman

unread,
Jun 20, 2009, 4:01:41 AM6/20/09
to

Further to my previous suggestion,

Reap[ Sow[#[[2]],{#[[1]]}]&/@data, _, {#1,Mean@#2}&][[2]]

will also give what you want, without needing 'data' to be sorted.

Ray Koopman

unread,
Jun 20, 2009, 4:01:29 AM6/20/09
to
On Jun 19, 5:45 pm, vJD <epho...@gmail.com> wrote:

data = {{{1,13},97.6493}, {{1,13},97.9511}, {{1,14},99.3002},
{{1,14},99.4602}, {{1,14},99.6193}, {{1,15},100.513},
{{1,15},101.149}, {{1,15},101.483}, {{1,15},101.494},
{{1,16},101.51}, {{1,16},101.895}};

{#[[1,1]],Mean[#[[All,2]]]}& /@ Split[data,#1[[1]]==#2[[1]]&]

{{{1,13},97.8002},{{1,14},99.4599},{{1,15},101.16},{{1,16},101.703}}

Note: if 'data' is not already sorted then use Split[Sort@data,...].

Bill Rowe

unread,
Jun 21, 2009, 7:03:03 AM6/21/09
to
On 6/19/09 at 8:46 PM, eph...@gmail.com (vJD) wrote:

>I have a nested list of this form:

>{{{1, 13}, 97.6493}, {{1, 13}, 97.9511}, {{1, 14}, 99.3002}, {{1,
>14}, 99.4602}, {{1, 14}, 99.6193}, {{1, 15}, 100.513}, {{1, 15},
>101.149}, {{1, 15}, 101.483}, {{1, 15}, 101.494}, {{1, 16}, 101.51},
>{{1, 16}, 101.895}}

>I want to shrink the list in the way that I calculate the mean over
>the second part for all elements where the first part is equal. so
>e.g. the first element of the new list will be {{1,13}, 97.8002},
>because they both have {1,13} and the mean of 97.6493 and 97.9511 is
>97.8002.

Here is one way

In[23]:= data = {{{1, 13}, 97.6493}, {{1, 13}, 97.9511}, {{1, 14},


99.3002}, {{1, 14}, 99.4602}, {{1, 14}, 99.6193}, {{1, 15},
100.513}, {{1, 15}, 101.149}, {{1, 15}, 101.483}, {{1, 15},

101.494}, {{1, 16}, 101.51}, {{1, 16}, 101.895}};

In[24]:= {#[[1, 1]], Mean[#[[All, 2]]]} & /@ SplitBy[data, First]

Out[24]= {{{1, 13}, 97.80019999999999}, {{1, 14}, 99.4599},
{{1, 15}, 101.15975}, {{1, 16}, 101.7025}}


Jens-Peer Kuska

unread,
Jun 21, 2009, 7:04:35 AM6/21/09
to
Hi,

data = {{{1, 13}, 97.6493}, {{1, 13}, 97.9511}, {{1, 14},


99.3002}, {{1, 14}, 99.4602}, {{1, 14}, 99.6193}, {{1, 15},
100.513}, {{1, 15}, 101.149}, {{1, 15}, 101.483}, {{1, 15},

101.494}, {{1, 16}, 101.51}, {{1, 16}, 101.895}};

and

{#, Mean[Last /@ Cases[data, {#, _}, Infinity]]} & /@
Union[First /@ data]

?

Regards
Jens

Andrzej Kozlowski

unread,
Jun 21, 2009, 7:01:33 AM6/21/09
to

On 20 Jun 2009, at 09:46, vJD wrote:

> I have a nested list of this form:
>
> {{{1, 13}, 97.6493}, {{1, 13}, 97.9511}, {{1, 14}, 99.3002}, {{1, 14},
> 99.4602}, {{1, 14}, 99.6193}, {{1, 15}, 100.513}, {{1, 15}, 101.149},
> {{1, 15}, 101.483}, {{1, 15}, 101.494}, {{1, 16}, 101.51}, {{1, 16},
> 101.895}}
>
> I want to shrink the list in the way that I calculate the mean over
> the second part for all elements where the first part is equal. so
> e.g. the first element of the new list will be {{1,13}, 97.8002},
> because they both have {1,13} and the mean of 97.6493 and 97.9511 is
> 97.8002.
>
> The problem is that there is no fixed number for the equal elements
> (so e.g. two elements with {1,13}, three elements with {1,14} and so
> on). The number is random.
>
> Can anybody please provide me with a statement or hint how to
> accomplish that task.
>
> Thank you very much
>
> Regards,
> Holger
>

In Mathematica 7 you can do this with:

ls={{{1, 13}, 97.6493}, {{1, 13}, 97.9511}, {{1, 14}, 99.3002}, {{1,

14},
99.4602}, {{1, 14}, 99.6193}, {{1, 15}, 100.513}, {{1, 15}, 101.149},
{{1, 15}, 101.483}, {{1, 15}, 101.494}, {{1, 16}, 101.51}, {{1, 16},
101.895}}

({#1[[1, 1]], Mean[#1[[All, 2]]]} & ) /@ GatherBy[ls, First]

{{{1, 13}, 97.80019999999999}, {{1, 14}, 99.4599},
{{1, 15}, 101.15975}, {{1, 16}, 101.7025}}

Andrzej Kozlowski


0 new messages