TITLE: Existence of an antiderivative function on an arbitrary subset of $\mathbb{R}$
QUESTION [23 upvotes]: Let $f:\mathbb{R}\to \mathbb{R}$ be  continuous at $x$ for every $x\in I$  where $I\subset \mathbb R$ could be arbitrary. Does there always exist a function $F:\mathbb{R}\to \mathbb{R}$ differentiable on $I$ and $F'(x) = f(x)$ for every $x \in I$?

The definition of a primitive is naturally defined on an interval. A mathematical curiosity is to understand the difficulties that can be encountered when trying to define this notion on any part of $\mathbb{R}$. 
A first difficulty is to try to find a good definition of the notion of a primitive on any part of $\mathbb{R}$. That was the purpose of this thread
"Correct definition of antiderivative function."
If we ask $F$ to be differentiable on an open set $J$ containing $I$, the thread "Existence of an antiderivative for a continuous function on an arbitrary subset of $\mathbb{R}$" gives a counterexample to the question.
If $I$ is an interval, the answer to the question is positive.  If $I$ is an open set, the answer to the question is also positive. (see comment)

REPLY [13 votes]: Please do not upvote this answer! It's just a detailed version of an answer that appeared on MO; if you feel the need to upvote something please upvote that answer instead.
$\newcommand\ui[2]{\overline{\int_{#1}^{#2}}}$
The obvious thing to try is $$F(x)=\int_0^x f(t)\,dt.$$Except "trying" that is stupid, since it's clear the integral need not exist. I thought about replacing $f$ by some nicer function $g$ such that $g(x)=f(x)$ if $f$ is continuous at $x$, as in a comment to the question on MO; couldn't make that work. The lightbulb Pietor Majer had is this: Use the upper Darboux integral, which we will denote $$\ui ab f(x)\,dx.$$
The beauty of this is that if $f:[a,b]\to\Bbb R$ is any function whatever then $\ui ab f$ exists, at least as an element of $[-\infty,\infty]$, and if $f$ is any bounded function then $\ui ab f\in\Bbb R$.
Of course the upper Darboux integral probably doesn't really deserve to be called an integral, since it's not linear. But it does retain a shred of linearity:


Lemma. If $a<b<c$ and $f:[a,c]\to\Bbb R$ is bounded then $\ui acf=\ui ab f+\ui bc f$.


Proof: Exercise. Easy or not, depending on who you are. If you get stuck you should be able to extract a proof from a proof that $\int_a^c=\int_a^b+\int_b^c$ for the Riemann integral. Note that of course you need to find a proof in a context where the author defined the RIemann integral in terms  of "upper and lower (Darboux) sums" instead of using Riemann sums. I suspect the proof in baby Rudin will qualify, not sure since I don't have a copy.
It follows that the upper Darboux integral satisfies a version of FTC sufficient for our purposes:


Lemma (UDI-FTC). Suppose $f:(a.b)\to\Bbb R$ is bounded and $p\in(a,b)$. Define $F:(a,b)\to\Bbb R$ by $F(x)=\ui pxf(t)\,dt$ (with the convention that $\ui\beta\alpha=-\ui\alpha\beta$). If $f$ is continuous at $x\in(a,b)$ then $F$ is differentiable at $x$ and $F'(x)=f(x)$.


Proof: Hint: The previous lemma shows that $$\frac{F(x+h)-F(x)}{h}
=\frac1h\ui x{x+h}f(t)\,dt;$$if $h$ is small then $f(t)$ is close to $f(x)$ on $[x,x+h]$ (or $[x+h,x]$, whichever makes sense).
If that's not enough note that $F'(x)=f(x)$ is explained in somewhat more detail in the answer on MO.
Of course it's  not clear how that helps, since $f$ is not bounded. But it's clear that $f$ is "locally bounded" on a neighborhood of $I$, and that that's enough. (This is the part where I'm adding details to the answer on MO, if anyone was wondering; at least one other person and I were both initially confused about this.)


Definition If $f:\Bbb R\to\Bbb R$ and $S\subset \Bbb R$ then $f$ is locally bounded on $S$ if for every $x\in S$ there exists $\delta>0$ such that $f$ is bounded on $(x-\delta,x+\delta)$.
Triviality If $f$ is locally bounded on $S$ and $K\subset S$ is compact then $f$ is bounded on $K$.
Corollary (UDI-FTC v2) The UDI-FTC above holds if we assume just that $f$ is locally bounded on $(a,b)$.


Proof: Given $x\in(a,b)$, choose $c,d$ with $a<c<d<b$ and $x,p\in(c,d)$. Since $f$ is bounded on $[c,d]$ the definition of $F(s)$ makes sense for $s\in(c,d)$, and the proof of the original UDI-FTC shows that $F'(x)=f(x)$.
And now we're in business:


Theorem. Suppose that $f:\Bbb R\to\Bbb R$, $I\subset \Bbb R$, and $f$ is continuous at $x$ for every $x\in I$. There exists $F:\Bbb R\to\Bbb R$ such that $F$ is differentiable at $x$ and $F'(x)=f(x)$ for every $x\in I$.


Proof. If $x\in I$ there exists an open interval $I_x$ such that $x\in I_x$ and $f$ is bounded on $I_x$. Let $$V=\bigcup_{x\in I}I_x.$$So $V$ is open, $I\subset V$, and $f$ is locally bounded on $V$.
Say $V=\bigcup_kJ_k$, where the $J_k$ are the connected components of $V$. UID-FTC v2 above shows that for each $k$ there exists $F_k:J_k\to\Bbb R$ such that $$F_k'(x)=f(x)\quad(x\in I\cap J_k).$$Define $F:\Bbb R\to\Bbb R$ by $$F(x)=\begin{cases}F_k(x),&(x\in J_k),
\\0,&(x\notin V).\end{cases}$$