All articles

KajoData / statistics

Quartiles and IQR: calculate and compare data spread

I work through the arithmetic and explain why two valid quantile methods can produce different IQR values.

Kajo RudzińskiPublished Updated 13 min read

Ordered cubes divided into four groups by gates, with the central groups highlighted.

When I look at order completion times, the average soon stops being enough. I want to know where the middle sits and how widely the central observations are spread. Quartiles and the interquartile range, or IQR, help answer those questions. They can distinguish a process that is usually consistent but occasionally very slow from one in which ordinary cases vary substantially.

I will use small synthetic datasets that you can follow on paper. Then I will explain why two valid calculations can return different quartiles and how to document a result so somebody else can reproduce it. Without that last step, it is surprisingly easy to spend a meeting hunting for a mistake that nobody made.

What Q1, Q2 and Q3 mean

Quartiles are values associated with the quarter points of ordered data. Q1 corresponds to the 25th percentile, Q2 to the 50th, and Q3 to the 75th. Q2 is the median. Intuitively, Q1 marks the lower quarter and Q3 the upper quarter, with the central part of the distribution between them. The Minitab documentation uses these percentile definitions too.

In a finite sample with repeated values, these boundaries do not necessarily split the records into four equally sized groups. If many orders take the same time, a numerical threshold cannot divide that shared value into separate pieces. A quartile value and a rule for assigning records to segments are different things. That distinction matters whenever somebody wants to label exactly one quarter of customers as a particular category.

Q1 has the units of the measured variable. If the variable is a duration in minutes, its quartiles are also in minutes. I would not label Q1 as 25%, because that percentage describes its position in the distribution, rather than the duration itself. A clear label prevents a reader from having to guess what the number on a dashboard means.

First define the observation

Suppose we have eight completed cases. Each duration is measured in whole minutes, from receipt to closure. These are synthetic data, ordered from smallest to largest:

1, 3, 3, 5, 8, 8, 8, 9

Each number represents one case. The three eights represent three observations and all remain in the calculation. Removing repeated values would change the question. Instead of describing cases, I would be describing a list of distinct durations. Before calculating anything, I also establish whether a missing duration means a failed measurement or a case that is still open.

A complete column does not automatically produce a representative report. Excluding open cases means that the report describes completed cases only. Some of the slowest cases may still be waiting for closure. I would state that selection rule beneath the result and report the number of open cases separately. Quartiles cannot repair a sampling or selection problem, however tidy the formula looks.

Calculate the medians of the two halves

For the first example, I choose a simple convention. With this even number of observations, I divide the sorted data into two equal halves and take the median of each half. This is the method for this example, rather than a promise that every statistical function will follow the same rule.

The median of all eight values lies between the fourth and fifth observations. Those are 5 and 8, giving Q2 = 6.5. The lower half is 1, 3, 3, 5. Its central values are 3 and 3, so Q1 = 3. The upper half is 8, 8, 8, 9. Its central values are also identical, giving Q3 = 8.

I can now describe the dataset without making the reader inspect a screenshot. The minimum is 1 minute, Q1 is 3 minutes, the median is 6.5 minutes, Q3 is 8 minutes and the maximum is 9 minutes. The value 6.5 does not occur in the original data, which is perfectly acceptable. A statistic describing position need not be one of the observed values.

Notice that the calculation uses positions after sorting. The original arrival order is irrelevant to these quartiles, although it may be important for analysing changes over time. I would keep timestamps in the underlying dataset rather than throwing them away after producing an ordered list. Sorting for a calculation and permanently replacing the original record sequence are separate decisions.

IQR is the distance between the quartiles

The formula is IQR = Q3 - Q1. In our first example, subtracting 3 from 8 gives 5 minutes. The interquartile range describes the width of the central part of the data. IQR itself is one number. The interval from Q1 to Q3 is a range of values, here from 3 to 8 minutes.

This distinction helps when writing an explanation. Saying “the IQR is from 3 to 8” mixes up an interval and its width. I would write: “Q1 is 3 minutes, Q3 is 8 minutes, and the IQR is 5 minutes.” That shows both the location of the central part and its spread. The reader does not have to work out whether a number is missing.

A small IQR means the central values are close together on the chosen scale. It does not establish that the process is fast enough. All cases could take a long time and still have very similar durations. I therefore show IQR alongside the median and the unit. A spread statistic alone does not tell us where the observations are located.

Why different methods return different answers

Consider a second synthetic dataset: 1, 2, 3, 4, 5, 6, 7, 8. The medians-of-halves method gives Q1 = 2.5 and Q3 = 6.5. Therefore IQR = 4. An alternative convention uses linear interpolation, locating a quantile with proportion p at position 1 + (n - 1) × p, with observations numbered from one.

For Q1, the position is 2.75. This is three quarters of the way from the second observation to the third, so the result is 2.75. Q3 falls at position 6.25 and equals 6.25. Under this convention the IQR is 3.5. Both calculations are internally consistent with their chosen rules. I do not switch conventions after seeing which result looks more convenient.

The R documentation describes nine quantile types, including its default type 7 with the interpolation just described. That is a good reason to compare settings before declaring one tool wrong. Differences are especially noticeable with small samples or large gaps between neighbouring observations. The word “quartile” by itself is insufficient documentation for reproducing a calculation.

Two trays with similar central clusters and a distant isolated cube

Documenting the calculation in Excel and pandas

In English Excel, QUARTILE.INC returns quartiles using the inclusive convention. For the values from 1 to 8 in A2:A9, =QUARTILE.INC(A2:A9,1) requests Q1. Change the last argument to 3 to request Q3. The corresponding results are 2.75 and 6.25.

These formulas use English function names and a comma argument separator. Regional settings may require a different separator, and a localized installation may use translated function names. Microsoft also documents QUARTILE.EXC, which follows a different quartile convention. I would not silently replace EXC with INC while maintaining a report. First establish which definition is required for comparison with earlier periods.

In pandas, Series.quantile accepts proportions between zero and one and an interpolation setting. For a Python report, I would record the selected setting in the metric definition. The people comparing results must also agree on filters, units and record level. Matching the quantile method will not help if one person analyses case durations while another analyses daily average durations.

Outliers are a reason to investigate

A common screening rule sets boundaries at Q1 - 1.5 × IQR and Q3 + 1.5 × IQR. In our first example, those boundaries are -4.5 and 15.5 minutes. No observation exceeds them. The negative lower boundary does not mean that negative durations are acceptable. It is the output of a statistical rule, which still needs interpretation in the context of the variable.

NIST describes these fences in its box plot guidance. I treat a point beyond a fence as a case to inspect. Perhaps somebody entered seconds into a minutes column. Perhaps the duration is correct and the case needed additional approval. The position of a dot cannot settle which explanation applies, and it does not authorize deleting the record.

If I replace 9 with 90 in the first dataset, Q1 and Q3 remain 3 and 8 under our medians-of-halves convention. IQR stays at 5 even though the maximum changes dramatically. That is why I also inspect the tail. If very long cases matter to the reader, describing the central observations alone will leave out part of the operational problem.

Reading a box plot

The box extends from Q1 to Q3, and a line inside it shows the median. Its length represents IQR. Check how the chosen tool draws the whiskers. In the variant using the 1.5 × IQR rule, they extend to the most extreme observations within the fences, rather than necessarily reaching the numerical fences themselves. The caption should identify the convention.

A box plot cannot reveal the exact arrangement of all the observations. Two datasets can share quartiles while having very different shapes inside the box. With a small sample I would add the individual points; with more observations I might include a histogram or another distribution view. I would not infer the number of peaks from the median’s position inside the box. This display compresses information and has practical limits as a result.

The width of the drawn box may also be a design choice rather than a sample size indicator. Some tools use variable widths and others use equal widths. Unless the chart explicitly states the encoding, I would read the printed sample counts instead of trying to estimate them from the geometry. A visually large box need not represent more records.

Comparing teams and periods

Suppose two teams both have a median duration of five minutes. One has Q1 = 4 and Q3 = 6; the other has Q1 = 2 and Q3 = 8. Their IQR values are 2 and 6 minutes respectively. I can say that the central durations are more dispersed in the second team. I cannot yet explain the difference through individual performance.

Before evaluating it, I would compare case types, operating hours, duration definitions and exclusion rules. If one team handles routine requests and another handles exceptions, a shared chart can be arithmetically correct and organizationally misleading. I would separate comparable cases and show the group sizes. A compact box based on three records deserves different caution from a similar box based on several thousand.

Comparing months also requires a stable quantile method. Switching functions can move Q1 without any change in the process. If I update the calculation, I record the change in the report definition and consider recalculating the comparison period. Otherwise the reader may interpret a change in analytical convention as a change in the company. This error is less conspicuous than a typo in a formula.

When IQR is zero

IQR can be zero even when the observations are not all identical. In the synthetic dataset 0, 0, 0, 0, 0, 0, 0, 8, the medians-of-halves method gives Q1 = 0 and Q3 = 0. The central part is concentrated at zero, although the last observation equals 8. Any later standardization that divides by IQR needs separate handling for this situation.

I would not substitute an arbitrary tiny number simply to make a formula stop complaining. First I would investigate what the zeros mean, how frequent each value is, and how the variable was measured. With count data or strongly rounded measurements, zero IQR can be a natural description. Additional information, such as the proportion of nonzero observations and their range, may be needed to reveal meaningful differences.

A value threshold does not specify a record count

Consider eight cases with durations 1, 1, 1, 1, 1, 1, 8, 9. Using medians of halves, Q1 is 1 and so is the median. A filter selecting durations less than or equal to Q1 returns six cases, substantially more than a quarter. The filter is behaving correctly: repeated values lie on its boundary. The report needs to explain whether a segment is defined by a duration threshold or by a fixed number of records.

To select exactly two of eight cases, I need an additional rule for ties. A case identifier could provide a secondary ordering, but the choice needs to be explicit. The quartile itself supplies no tie-breaking rule. I would not call two selected cases uniquely the “bottom 25%” when four other cases have the same duration. This matters for rankings, discounts and customer segments too.

Round for display after calculating

Interpolation can produce fractional quartiles even when every observation is an integer. Rounding Q1 and Q3 before subtracting may give a different IQR from rounding the final difference. I keep the available precision during calculation and choose display precision separately. Decimal places should help the reader without implying more measurement accuracy than the source provides.

When durations were recorded in whole minutes, a statistic of 6.5 minutes does not mean every case was timed to half-minute accuracy. Nor does a small quartile movement after one new observation prove the process improved. I would inspect the sample size, comparison scope and raw observations first. One record matters much more in a dataset of eight cases than in a large, stable collection.

I also check the source unit against the chart labels. Converting minutes to seconds multiplies durations, quartiles and IQR by 60. An IQR of 5 minutes and one of 300 seconds describe the same spread. Put them beside each other without units and they look radically different. A correct label is part of making the calculation usable.

For a shared report, I keep the metric definition accessible to readers. It should identify the source, date range, exclusions, unit and quantile method. When two numbers disagree, these are the first details I compare. Only after establishing identical inputs does it make sense to investigate differences between functions and decide whether they affect the business interpretation.

An exercise before your next report

Write the numbers from 1 to 8 on paper. Calculate the median, the quartiles using medians of halves, and IQR. Then use the position formula 1 + (n - 1) × p. You should obtain the same median, 4.5, but two different IQR values: 4 and 3.5. Explain the difference in one sentence. This checks understanding more effectively than copying a function call.

Next, change the largest value to 80 and repeat both quartile calculations. Before calculating, predict which interpolation positions will change and which observed values they refer to. The exercise helps separate a change in an extreme value from a change in the central order statistics. Keep the convention fixed throughout each comparison, and describe exactly what you changed in the input.

Finally, add three statements to your example report: what a record represents, how missing measurements are treated, and which quantile convention is used. For spread around the mean, continue with standard deviation. These measures can complement one another, but they answer different questions about the data. I choose the question before choosing the chart colour.

Key takeaways

How do I calculate IQR?

Subtract Q1 from Q3: IQR = Q3 - Q1. Both quartiles must use the same method and dataset.

Why does Excel disagree with medians of halves?

Several quantile conventions exist. QUARTILE.INC uses interpolation, so small datasets may produce a different result from medians of halves.

Should I delete points beyond 1.5 times IQR?

Investigate them first. A flagged point may be an error, a valid exceptional case or a sign that another description is needed.

AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.