Suppose that the results of a measurement (of let's say the number of citizens of a town, which is modeled by the random variable $X$) are given as follows:
Out of 4000 towns:
a) 500 have less than 10000 citizens;
b) 1500 have between 10000 and 100000 citizens;
c) 1500 have between 100000 and 1000000 citizens;
d) 500 have more than 1000000 citizens
My question is: how would I be able to calculate the average number of citizens per town (i.e. $\bar X$)?
The usual way to estimate the mean is to take the midpoint of each class as the representative value for each class (however inaccurate the estimate is then likely to be) simply because you have no more accurate information.
So we would take $5000$ as the value for the first class, $55000$ for the second class, and so on, and work out an estimate from there, i.e. $$\bar{X}\simeq\frac{500\times5000+1500\times55000+...}{4000}$$