title: Explain the quantile() function in R
body: <p>I've been mystified by the R quantile function all day.  </p>
  
  <p>I have an intuitive notion of how quantiles work, and an M.S. in stats, but boy oh boy, the documentation for it is confusing to me.  </p>
  
  <p>From the docs:</p>
  
  <blockquote>
    <p>Q[i](p) = (1 - gamma) x[j] + gamma
    x[j+1],</p>
  </blockquote>
  
  <p>I'm with it so far.  For a type <em>i</em> quantile, it's an interpolation between x[j] and x [j+1], based on some mysterious constant <em>gamma</em></p>
  
  <blockquote>
    <p>where 1 &lt;= i &lt;= 9, (j-m)/n &lt;= p &lt;
    (j-m+1)/ n, x[j] is the jth order
    statistic, n is the sample size, and m
    is a constant determined by the sample
    quantile type. Here gamma depends on
    the fractional part of g = np+m-j. </p>
  </blockquote>
  
  <p>So, how calculate j?   m?</p>
  
  <blockquote>
    <p>For the continuous sample quantile
    types (4 through 9), the sample
    quantiles can be obtained by linear
    interpolation between the kth order
    statistic and p(k): </p>
    
    <p>p(k) = (k - alpha) / (n - alpha - beta
    + 1),
    where α and β are constants determined
    by the type. Further, m = alpha + p(1
    - alpha - beta), and gamma = g.</p>
  </blockquote>
  
  <p>Now I'm really lost.  p, which was a constant before, is now apparently a function.  </p>
  
  <p>So for Type 7 quantiles, the default...</p>
  
  <blockquote>
    <p>Type 7</p>
    
    <p>p(k) = (k - 1) / (n - 1). In this case, p(k) = mode[F(x[k])]. This is used by S. </p>
  </blockquote>
  
  <p>Anyone want to help me out?  In particular I'm confused by the notation of p being a function and a constant, what the heck <em>m</em> is, and now to calculate j for some particular <em>p</em>.  </p>
  
  <p>I hope that based on the answers here, we can submit some revised documentation that better explains what is going on here.  </p>
  
  <p><a href="https://svn.r-project.org/R/trunk/src/library/stats/R/quantile.R" rel="noreferrer">quantile.R source code</a>
  or type:  quantile.default</p>