Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Average the same elements of the list
Replies: 4   Last Post: May 24, 2013 5:24 AM

 Messages: [ Previous | Next ]
 Daniel Posts: 59 Registered: 7/24/12
Re: Average the same elements of the list
Posted: May 24, 2013 5:24 AM

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}}

> 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!
>

Date Subject Author
5/23/13 Bob Hanlon
5/23/13 Tomas Garza Hernandez
5/23/13 Murray Eisenberg
5/24/13 Daniel