TITLE: Topological spaces admitting an averaging function
QUESTION [68 upvotes]: Let $M$ be a topological space. Define an averaging function as a continuous map $f:M \times M \to M$ which satisfies $f(a,b) = f(b,a)$ for all $a,b \in M$ and $f(a,a) = a$ for all $a \in M$.
These seem like reasonable properties for a function which deserves the name "average" to have, since they are enjoyed by the usual averaging functions on $\mathbb{R}^n$.  
One can rule out an averaging function on both $S^1$ and $S^2$ by some cohomological arguments.  I have not worked out whether any sphere admits an averaging function, but I suspect not.  
Euclidean spaces all do (given by the usual average). 
Does anyone have a characterization of spaces which admit an averaging function?

REPLY [10 votes]: The paper 

B.Eckmann, Räume mit Mittelbildungen, Comment.Math.Helv. 28(1954),
  329-340

contains most of the answers given so far. The results of this paper are summarized in the following very interesting note of Benno Eckmann:

B. Eckmann, Social choice and topology.

where he defines $n$-means as symmetric functions $\prod_{i=1}^nX\to X$ such that precomposing with the diagonal gives the identity. The means in this mathoverflow question are, in this new language, $2$-means. He proves:
Theorem 6: If a finite polyhedron admits $n$-means for some $n$, then it is contractible.
Theorem 7 + Theorem 4: A polyhedron $X$ admits $n$-means for all $n$ if and only if either it is contractible or it has the homotoy type of a product of rational Eilenberg Mac Lane spaces.
Theorem 2. If a space $X$ admits an n-mean for some n ≥ 2 then $\pi_1(X)$ is Abelian, multiplication by n is an automorphism of $\pi_k(X)$ for all $k$, and $\pi_k(X)$ is uniquely divisible by $n$.