I'm working through a statistics textbook while learning R and I've run into a stumbling block on the following example:



After looking at ?quantile I attempted to recreate this in R with the following:

> nuclear <- c(7, 20, 16, 6, 58, 9, 20, 50, 23, 33, 8, 10, 15, 16, 104)
> quantile(nuclear)
   0%   25%   50%   75%  100% 
  6.0   9.5  16.0  28.0 104.0 


Given that the text and R have different results, I'm gathering that R is utilizing the median in the calculation of the first and third quartiles.

Question:

Should I include the median in calculating the first and third quartiles?

More specifically, does the textbook or R have this correct? If the textbook has this correct, is there a way to properly achieve this in R?

Thanks in advance.

Your textbook is confused.  Very few people or software define quartiles this way.  (It tends to make the first quartile too small and the third quartile too large.)

The quantile function in R implements nine different ways to compute quantiles!  To see which of them, if any, correspond to this method, let's start by implementing it.  From the description we can write an algorithm, first mathematically and then in R:


Order the data $x_1 \le x_2 \le \cdots \le x_n$.
For any set of data the median is its middle value when there are an odd number of values; otherwise it is the average of the two middle values when there are an even number of values.  R's median function calculates this.

The index of the middle value is $m = (n+1)/2$. When it is not an integer, $(x_l + x_u)/2$ is the median, where $l$ and $u$ are $m$ rounded down and up.  Otherwise when $m$ is an integer, $x_m$ is the median.  In that case take $l=m-1$ and $u=m+1$.  In either case $l$ is the index of the data value immediately to the left of the median and $u$ is the index of the data value immediately to the right of the median.
The "first quartile" is the median of all $x_i$ for which $i \le l$.  The "third quartile" is the median of $(x_i)$ for which $i \ge u$.


Here is an implementation.  It can help you do your exercises in this textbook.

quart <- function(x) {
  x <- sort(x)
  n <- length(x)
  m <- (n+1)/2
  if (floor(m) != m) {
    l <- m-1/2; u <- m+1/2
  } else {
    l <- m-1; u <- m+1
  }
  c(Q1=median(x[1:l]), Q3=median(x[u:n]))
}


For instance, the output of quart(c(6,7,8,9,10,15,16,16,20,20,23,33,50,58,104)) agrees with the text:

Q1 Q3 
 9 33 


Let's compute quartiles for some small datasets using all ten methods: the nine in R and the textbook's:

y <- matrix(NA, 2, 10)
rownames(y) <- c("Q1", "Q3")
colnames(y) <- c(1:9, "Quart")
for (n in 3:5) {
  j <- 1
  for (i in 1:9) {
    y[, i] <- quantile(1:n, probs=c(1/4, 3/4), type=i)
  }
  y[, 10] <- quart(1:n)
  cat("\n", n, ":\n")
  print(y, digits=2)
}


When you run this and check, you will find that the textbook values do not agree with any of the R output for all three sample sizes.  (The pattern of disagreements continues in cycles of period three, showing that the problem persists no matter how large the sample may be.)

The textbook might have misconstrued John Tukey's method of computing "hinges" (aka "fourths").  The difference is that when splitting the dataset around the median, he includes the median in both halves.  That would produce $9.5$ and $28$ for the example dataset.
