TITLE: Asymptotic for the average of $|d(n)-\log n|$?
QUESTION [10 upvotes]: 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.$$

REPLY [7 votes]: 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).$$