title: When values have different sampling probabilities, what is the fastest way to calculate the median?
body: <p>Consider this toy example:</p>
  
  <p>A teacher wants to calculate the median height of students in his class. But not all students show up to class every day so on any given day, the calculated median height might be different. Their probabilities of being in class and their heights are given in the table below. Given this information, he could estimate the expected median. </p>
  
  <pre><code>&gt;set.seed(123)
  &gt;data1 &lt;- data.frame(Student=c(LETTERS[1:10]), Height.cm=sort( rnorm(n=10, mean=140, sd=10)), Prob.in.class=c(1,.75,1,.5,1,1,1,.25,1,.5))
  
  &gt;data1
  
     Student Height.cm Prob.in.class
  1        A  127.3494          1.00
  2        B  133.1315          0.75
  3        C  134.3952          1.00
  4        D  135.5434          0.50
  5        E  137.6982          1.00
  6        F  140.7051          1.00
  7        G  141.2929          1.00
  8        H  144.6092          0.25
  9        I  155.5871          1.00
  10       J  157.1506          0.50
  </code></pre>
  
  <p>What is the fastest way in R to estimate the median (or arbitrary quantiles) of a distribution like this?</p>
  
  <p>For my actual calculations, I'll need to estimate the medians and arbitrary quantiles on hundreds of different vectors with tens of thousands of points (and associated probabilities) each. I've seen this <a href="https://stats.stackexchange.com/questions/265866/calculating-medians-and-quartiles-of-arbitrary-probability-density-functions">suggestion</a> where the probability density function is estimated using the trapezoidal method but I'm not sure this is the best way.  </p>
  
  <p>Any advice you could provide would be greatly appreciated. Thanks!</p>