title: how to calculate the median on grouped dataset?
body: <p>My dataset is as following:</p>
  <pre><code>salary  number
  1500-1600   110
  1600-1700   180
  1700-1800   320
  1800-1900   460
  1900-2000   850
  2000-2100   250
  2100-2200   130
  2200-2300   70
  2300-2400   20
  2400-2500   10
  </code></pre>
  <p>How can I calculate the median of this dataset? Here's what I have tried:</p>
  <pre><code>x &lt;- c(110, 180, 320, 460, 850, 250, 130, 70, 20, 10)
  colnames &lt;- &quot;numbers&quot;
  rownames &lt;- c(&quot;[1500-1600]&quot;, &quot;(1600-1700]&quot;, &quot;(1700-1800]&quot;, &quot;(1800-1900]&quot;, 
                &quot;(1900-2000]&quot;, &quot;(2000,2100]&quot;, &quot;(2100-2200]&quot;, &quot;(2200-2300]&quot;,
                &quot;(2300-2400]&quot;, &quot;(2400-2500]&quot;)
  y &lt;- matrix(x, nrow=length(x), dimnames=list(rownames, colnames))
  data.frame(y, &quot;cumsum&quot;=cumsum(y))
  
              numbers cumsum
  [1500-1600]     110    110
  (1600-1700]     180    290
  (1700-1800]     320    610
  (1800-1900]     460   1070
  (1900-2000]     850   1920
  (2000,2100]     250   2170
  (2100-2200]     130   2300
  (2200-2300]      70   2370
  (2300-2400]      20   2390
  (2400-2500]      10   2400
  </code></pre>
  <p>Here, you can see the half-way frequency is <code>2400/2</code>=<code>1200</code>. It is between <code>1070</code> and <code>1920</code>. Thus the <strong>median class</strong> is the <code>(1900-2000]</code> group. You can use the formula below to get this result:</p>
  <blockquote>
  <p>Median = L + h/f (n/2 - c)</p>
  </blockquote>
  <p>where:</p>
  <blockquote>
  <p><strong>L</strong> is the lower class boundary of median class<br />
  <strong>h</strong> is the size of the median class i.e. difference between upper and lower class boundaries of median class<br />
  <strong>f</strong> is the frequency of median class<br />
  <strong>c</strong> is previous cumulative frequency of the median class<br />
  <strong>n/2</strong> is total no. of observations divided by 2 (i.e. sum <strong>f</strong> / 2)</p>
  </blockquote>
  <p>Alternatively, <strong>median class</strong> is defined by the following method:</p>
  <blockquote>
  <p>Locate n/2 in the column of cumulative frequency.</p>
  <p>Get the class in which this lies.</p>
  </blockquote>
  <p>And in code:</p>
  <pre><code>&gt; 1900 + (1200 - 1070) / (1920 - 1070) * (2000 - 1900)    
  [1] 1915.294
  </code></pre>
  <p>Now what I want to do is to make the above expression more elegant - i.e. <code>1900+(1200-1070)/(1920-1070)*(2000-1900)</code>. How can I achieve this?</p>