Asymptotic for the average of $|d(n)-\log n|$? Let $d(n)$ be the number of positive integers that divide $n$. It is well known that $d(n)$ is on average $\log n$. However, it is also well known that for most $n$ the number $d(n)$ is rather close to $(\log n)^{\log 2}$. What explains the anomaly is that the values of $d(n)$ that are larger than its typical value are typically way larger, hence, they dominate the first moment. The average of $d^2(n)$ is $\gg (\log n)^3$, which is just another manifestation of the same anomaly.
To understand these things a little better I was interested in estimating the average $$ R(x):= \sum_{1\leq n \leq x } | d(n)-\log n|.$$ Has anyone seen an asymptotic for this in the literature?
As Alexander Kalmynin wrote in the comments one has
$$R(x)=\sum_{1\leq n \leq x } |d(n)-\log n| \ll \sum_{n\leq x } d(n) +x\log x\ll x\log x
.$$ Furthermore, for each $\epsilon>0$ there exists a subset $A_\epsilon\subset \mathbb N$ of density $1$ with $d(n)\leq (\log n)^{\log 2+\epsilon}$ for all $n\in A_\epsilon$, we get $$ R(x) \geq \sum_{n\in A_\epsilon\cap[1,x]} ((\log n)- (\log n)^{9/10}) \gg x \log x.$$ Hence, $$ x\log x\ll R(x) \ll x \log x.$$
 A: Here is a variation of Lucia's nice argument. Writing
$$|d(n)-\log n|=d(n)+\log n-2\min(d(n),\log n),$$
we see that
$$R(x)=2x\log x+O(x)-2\sum_{n\leq x}\min(d(n),\log n).$$
Now let $\kappa\in(0,1)$ be fixed, and let us use that $\min(d(n),\log n)\leq d(n)^\kappa(\log n)^{1-\kappa}$. Then, by Theorem 2 in Chapter II.6 of Tenenbaum: Introduction to analytic and probabilistic number theory, we get
$$\sum_{n\leq x}\min(d(n),\log n)\leq(\log x)^{1-\kappa}\sum_{n\leq x}2^{\kappa\Omega(n)}\ll_\kappa x(\log x)^{2^\kappa-\kappa}.$$
The value $\kappa:=-\log\log 2/\log 2\approx 0.529$ yields the minimal exponent $$2^\kappa-\kappa=(1+\log\log 2)/\log 2\approx 0.914,$$
whence
$$R(x)=2x\log x + O\left(x(\log x)^{(1+\log\log 2)/\log 2}\right).$$
A: Nice question, with an amusing answer
$$ 
R(x) \sim 2 x\log x, 
$$
so that the trivial upper bound that you get from the triangle inequality is tight.  The point is that the average of the divisor function is captured by a small set of numbers $n$ where $d(n)$ is large, while for most $n$ the divisor function is negligible in comparison to $\log n$.
To give a quick proof, let ${\mathcal N}$ denote the set of integers below $x$ with $d(n) >(\log x)^{1-\epsilon}$.  Since $d(n) \le 2^{\Omega(n)}$ (here $\Omega(n)$ is the number of primes dividing $n$ counted with multiplicity) one must have $\Omega(n) \ge (1-\epsilon) \log \log x/\log 2$ for $n\in {\mathcal N}$, and since $\Omega(n)$ has normal order $\log \log x$, it follows that $|{\mathcal N}|=o(x)$.   Note however that
$$ 
\sum_{n\in {\mathcal N}} d(n) = \sum_{n\le x} d(n) - \sum_{\substack{n\le x \\ n\not\in {\mathcal N}}} d(n) = x\log x +O(x) + O(x (\log x)^{1-\epsilon})\sim x\log x. 
$$
Therefore
$$
\sum_{n\in {\mathcal N}} |d(n)-\log n| = \sum_{n\in {\mathcal N}} d(n) + O(|{\mathcal N}| \log x) \sim x\log x. 
$$
On the other hand
$$ 
\sum_{\substack{n\le x\\ n\not\in {\mathcal N}}} |d(n) -\log n| =  \sum_{\substack{n\le x\\ n\not\in {\mathcal N}}}(\log n + O((\log x)^{1-\epsilon})) \sim x\log x.
$$
The claimed asymptotic follows.
