TITLE: Examples of non-continuous, non-piecewise-constant, idempotent map?
QUESTION [12 upvotes]: So a projection $P$ is a linear map such that $P^2 = P$. If we don't require linearity, then there are other examples of functions $f$ such that $f^2 = f$. For example, the floor and ceiling functions. If $f: \mathbb{R} \to \mathbb{R}$ and we require continuity, it seems that the image is a closed interval and $f(x)=x$ for all $x$ in the image.
I'm wondering if there are any examples of idempotent maps that are not continuous but also not piecewise constant?

REPLY [10 votes]: This a “frame challenge” answer — it seems the restrictions in the question are partly motivated by a misconception, and so this doesn’t answer the question as written, but may give another answer to its original motivation.  The question says “If $f : \newcommand{\R}{\mathbb{R}}\R \to \R$ and we require continuity, it seems that we basically get the identity function”, and it seems this is the motivation for including “non-continuous” in the question.
However, continuous idempotents don’t really have to be close to the identity function, except to the same extent that all idempotents do.  The question links the claim to a nice answer which shows that:


If $f:\R \to \R$ is continuous and idempotent then $I=f(\R)$ is a closed interval and $f(x)=x$ for all $x\in I$.  
If $f$ is also differentiable and nonconstant, then $I=\R$, i.e., $f(x)=x$ for all $x\in\mathbb R$.


which is true, but a little misleading — regrouping makes the dependency of the conditions clearer:

Any idempotent map $f$ on any set $X$ acts as the identity on its image $f(X) \subseteq X$.  
The image of any continuous idempotent $f:\R \to \R$ is a closed interval.
The image of any differentiable idempotent $f:\R \to \R$ is either a point (i.e. $f$ is constant), or the whole of $\R$ (in which case by (1), $f$ is the identity function).

That is, all idempotents are somewhat like the identity; and non-constant differentiable idempotents are precisely the identity; but continuity doesn’t force the idempotent to be any more identity-like than it already had to be. So if the original motivation was partly just “interesting, non-trivial examples of idempotents”, then there’s no need to exclude continuous ones.
A nice intuitive continuous example is the “absolute value” function, $x \mapsto \left|x\right|$.
Another nice one is the function $ x \mapsto \left\{ \begin{array}{lr}x & -1 \leq x \leq 1 \\ \frac{1}{x} & \text{otherwise}\end{array}\right. $

(Graph from WolframAlpha)
N.S.’s answer gives a general explanation: an idempotent function on $\mathbb{R}$ can have an arbitrary subset $S$ of $\mathbb{R}$ as its image (and on $S$ it must act as the identity), and then on the rest of $\mathbb{R}$, it can be an arbitrary function $\mathbb{R} \setminus S \to S$.  So for continuous examples, take $S$ to be any closed interval, and then $f$ on $\R \setminus S$ (which must be the union of 0, 1, or 2 half-infinite open intervals) can be any continuous function into $S$ that fixes the endpoints of $S$.