Dimensional analysis - application to logarithms I read some nice threads about this topic:
physics StackExchange
maths StackExchange
stats StackExchange
However, it still puzzles me that logarithm of some physical quantity has no units.
Example, let's assume we have a collection of values of the distance between two cities.
In set A, distances are expressed in km, while in set B distances are expressed in m.
If I apply a logarithmic transformation to both sets and compute the average and standard deviation I get two different values for the first parameter and the same for the latter.
The latter makes sense because I'm subracting logarithms and that is equal to a division operation so I get dimensionless values for the standard deviation.
For the average value I get different values. Assuming that the logarithmic transformation returns dimensionless values in principle I could sum the average values of sets A and B and get a number, however this doesn't make sense since sets A and B are not expressed in the same scale of length unit. Therefore one can argue that you must still somehow keep track of dimensions when you do logarithmic transformations or you might end up summing apples and oranges.
What is your take on the above?
 A: The reason a logarithmic function, or an exponential function can't have dimensions is easy to see if you consider what the expression for a logarithm is in terms of a power series.
$$
\begin{align}
 \ln x &= (x-1) - \frac{(x-1)^2}{2} + \frac{(x-1)^3}{3} + \cdots\\
       &= \sum\limits_{n=1}^\infty \left((-1)^{n-1}\frac{(x-1)^n}{n}\right)
\end{align}
$$
If $x$ has dimensions (say of length), then it's clear that $\ln x$ is just a nonsensical physical quantity because its dimensions make no sense at all.  So $x$, and hence $\ln x$, must be pure numbers.
A similar argument applies to exponentials: 
$$\exp x = \sum\limits_{n=0}^\infty \frac{x^n}{n!}$$
So $x$, and $e^x$ must be dimensionless.  This also applies to trig functions of course.
In particular this means you can't take the logarithm or exponential of any physical quantity: you can only ever take logarithms or exponentials or ratios of physical quantities, which are pure numbers.
Here's an example of taking logarithms in a legitimate way.  If we have some quantity with a dimension, $q$, we can express it as $q = xu$ where $x$ is a pure number and $u$ is a unit of the same dimension as $q$.  So if we want a quantity with the dimension of length we can express it as $d\,\mathrm{mi}$, where $\mathrm{mi}$ is a mile.  So for any quantity with a dimension we can construct a pure number by dividing by the unit:
$$\frac{d\,\mathrm{mi}}{1\,\mathrm{mi}} = d$$
And it's fine to take logs of this.  And using this technique we can do things like combining logarithms of quantities with different units:
$$\ln\left(\frac{x\,\mathrm{chain}}{1\,\mathrm{chain}}\right)
 + \ln\left(\frac{y\,\mathrm{furlong}}{1\,\mathrm{furlong}}\right)
  = \ln \left(xy \frac{\mathrm{chain}\,\mathrm{furlong}}{\mathrm{chain}\,\mathrm{furlong}}\right)
$$
(A $\mathrm{chain}\,\mathrm{furlong}$ is an acre.)
The units don't have to be dimensionally the same even in cases like this
$$\ln\left(\frac{A\,\mathrm{acre}}{1\,\mathrm{acre}}\right)
 + \ln\left(\frac{m\,\mathrm{month}}{1\,\mathrm{month}}\right)
  = \ln \left(Am \frac{\mathrm{acre}\,\mathrm{month}}{\mathrm{acre}\,\mathrm{month}}\right)
$$
Acre months might be a useful unit for computing rent on land, say.
A: 
Example, let's assume we have a collection of values of the distance between two cities. In set A, distances are expressed in km, while in set B distances are expressed in m. [I then] apply a logarithmic transformation to both sets...

Sure, but you're not taking the logarithms of the distances. Instead, you have a set $S = \{x_j : j=1,\ldots,N\}$ of a bunch of different distances, and you have two different reference lengths, $R_1 = 1\:\rm km$ and $R_2 = 1\:\rm m$, and you're then forming the new sets
\begin{align}
\tilde S_1 & = \left\{ \ln(x_j/R_1) : j=1,\ldots,N \right\}
\quad \text{and} \\
\tilde S_2 & = \left\{ \ln(x_j/R_2) : j=1,\ldots,N \right\},
\end{align}
since "take the logarithm of $x_j$ when expressed in kilometers" really means "take the quotient of the length $x_j$ and the length $R_1=1\:\rm km$, and then take the logarithm of that value".
These are different sets of numbers, so in principle there's no reason to expect them to be related.
Fortunately, however, the properties of the logarithm do allow us to say useful things about the relationship between these two transformed sets, since for every distance $x_j$ we have
$$
\ln\left(\frac{x_j}{R_2}\right)
=
\ln\left(\frac{x_j}{R_1}\frac{R_1}{R_2}\right)
=
\ln\left(\frac{x_j}{R_1}\right) + \ln\left(\frac{R_1}{R_2}\right),
$$
where we know that $\frac{R_1}{R_2}=1000$, so therefore $\ln\left(\frac{R_1}{R_2}\right)=\ln(1000)=3\ln(10) \approx 6.9$. 
In other words, the sets $\tilde S_1$ and $\tilde S_2$ are related to each other by a rigid translation; this makes a lot of sense, as the sets $\{x_j/R_1 : j=1,\ldots,N\}$ and $\{x_j/R_2 : j=1,\ldots,N\}$ are related by a scaling transformation, and logarithms turn scalings into translations.
In particular, the average of $\tilde S_1$, $\mathbb E(\tilde S_1)$ will be directly related to $\mathbb E(\tilde S_1)$ via
$$
\mathbb E(\tilde S_2) = \mathbb E(\tilde S_1) + \ln(1000).
$$
Moreover, since translations don't affect the shape of the distribution, the standard deviation of both sets will be identical.

As for the more general question of why you cannot take logarithms of the $x_j$ directly, that's well-trod territory with a large number of answers on this site. See, for instance, my answer to Why is it "bad taste" to have a dimensional quantity in the argument of a logarithm or exponential function?, as well as the many questions on the Linked sidebar on the right.
A: The log is an exponent: if $p=\log x$ then $e^p=x$.  There's nothing fundamentally different between one exponent or the other, and you would never think of the exponent of $t^2$ in $x(t)=x_0+v_0t+\frac{1}{2}at^2$ as having units.  
When taking the log of $A$ and $B$, you need to express $A$ and $B$ in the same unit - say $km$ in your case.  One then takes the log of not of the unitful $A$ but of the unitless $A/km$
In the case of $x_0+v_0t+\frac{1}{2}at^2$, it wouldn't make sense to obtain an equation for $x(t)$ if some of the time measurements were in seconds and others in microseconds: you'd convert everything to a single unit of time first, and only then you look for the power law: the exponent shouldn't depend on using seconds or microseconds, and the units are "put back" when expressing $x$ in meters, $v$ in $m/s$, $t$ in seconds etc.
