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

Average the same elements of the list

17 views
Skip to first unread message

BBabic

unread,
May 23, 2013, 3:19:59 AM5/23/13
to
Hello,
I have list which is something like
data={
{{a1,b1,c1},d1},{{a2,b2,c2},d2}}
I would like to get new list which gets average of the second elements if the first elements in the sublists are all the same.
Namely if a1=a2,b1=b2,c1=c2
new list would look like
datanew={{a1,b1,c1},Mean[{d1,d2}]
Is there an elegant way to do this ?
Thanks!

BBabic

unread,
May 23, 2013, 3:16:14 AM5/23/13
to

Tomas Garza

unread,
May 23, 2013, 3:51:14 AM5/23/13
to

Elegance is often a matter of individual taste (it lies in the eyes of the
beholder?), but a possible way is
datanew:=If[data[[1,1]]==data[[2,1]],{data[[1,1]],Mean[{data[[1,2]],data[[2,2]]}]},data]
data={{{1,2,3},4},{{1,2,3},5}}{{{1,2,3},4},{{1,2,3},5}}
datanew{{1,2,3},9/2}
data={{{1,1,3},4},{{1,2,3},5}}{{{1,1,3},4},{{1,2,3},5}}
datanew{{{1,1,3},4},{{1,2,3},5}}
-Tomas
> From: bipsi...@gmail.com
> Subject: Average the same elements of the list
> To: math...@smc.vnet.net
> Date: Wed, 22 May 2013 02:18:09 -0400

Ray Koopman

unread,
May 23, 2013, 3:49:53 AM5/23/13
to
datanew = {data[[1,1]], Mean@Pick[data[[All,2]],data[[All,1]],data[[1,1]]]}

Murray Eisenberg

unread,
May 23, 2013, 3:53:37 AM5/23/13
to
You didn't specify what to return in case the first elements of the
sublists are NOT all the same; let's return the entire original list in
this case. Then perhaps something like the following will do.

op[lis_] :=
If[And @@ MapThread[Equal, First /@ lis],
{First@First@lis, Mean[Last /@ lis]},
lis]

op[{ {{2, 5, -1}, d1}, {{2, 5, -1}, d2}}]
(* { {2, 5, -1}, (d1 + d2)/2 } *)

op[{{{2, 5, -1}, d1}, {{2, 9, -1}, d2}}]
(* {{{2, 5, -1}, d1}, {{2, 9, -1}, d2}} *)

Undoubtedly the code can be shortened.

On May 22, 2013, at 2:18 AM, BBabic <bipsi...@gmail.com> wrote:

> Hello,
> I have list which is something like
> data={
> {{a1,b1,c1},d1},{{a2,b2,c2},d2}}
> I would like to get new list which gets average of the second elements if the first elements in the sublists are all the same.
> Namely if a1=a2,b1=b2,c1=c2
> new list would look like
> datanew={{a1,b1,c1},Mean[{d1,d2}]
> Is there an elegant way to do this ?
> Thanks!

---
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2838 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305






Bill Rowe

unread,
May 23, 2013, 3:54:58 AM5/23/13
to
On 5/22/13 at 2:18 AM, bipsi...@gmail.com (BBabic) wrote:

>Hello, I have list which is something like data={
>{{a1,b1,c1},d1},{{a2,b2,c2},d2}} I would like to get new list which
>gets average of the second elements if the first elements in the
>sublists are all the same. Namely if a1=a2,b1=b2,c1=c2 new list
>would look like datanew={{a1,b1,c1},Mean[{d1,d2}] Is there an
>elegant way to do this ? Thanks!

Something like

{#[[1,1]],Mean[#[[All,-1]]]}/@GatherBy[data/.{a__,d_}->{{a},d}]

should work


Bob Hanlon

unread,
May 23, 2013, 3:55:59 AM5/23/13
to

data = {{{a1, b1, c1}, d1}, {{a2, b2, c2}, d2},
{{a1, b1, c1}, d3}, {{a2, b2, c2}, d4},
{{a1, b1, c1}, d5}, {{a3, b3, c3}, d7}};


For v7 and later


sol = Plus @@ #/Length[#] & /@
GatherBy[data, First]


{{{a1, b1, c1}, (1/3)*(d1 + d3 + d5)},
{{a2, b2, c2}, (d2 + d4)/2}, {{a3, b3, c3}, d7}}


or (probably faster for longer lists)


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


True


For v3 or later


sol == (Plus @@ #/Length[#] & /@
Split[data // Sort, #1[[1]] == #2[[1]] &])


True



Bob Hanlon

Daniel

unread,
May 24, 2013, 5:24:40 AM5/24/13
to
data={{{a1,b1,c1},d1},{{a2,b2,c2},d2}};
{#[[1, 1]], #[[All, 2]] // Mean} & /@ SplitBy[data, First]

output:
{{{a1, b1, c1}, d1}, {{a2, b2, c2}, d2}}

data={{{a1,b1,c1},d1},{{a1,b1,c1},d2}};
{#[[1, 1]], #[[All, 2]] // Mean} & /@ SplitBy[data, First]

output:
{{{a1, b1, c1}, (d1 + d2)/2}}
0 new messages