You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言中四分位数计算结果存在歧义的问题求助

Understanding R's Quartile Calculation in summary()

Great question—this discrepancy happens because R uses a specific algorithm to calculate quartiles by default, which differs from the "intuitive" manual splitting method you might have used to get your expected values. Let’s break this down step by step.

First, let's recap your data

Your sorted dataset is:
data <- c(55, 64, 68, 74, 78, 80, 84, 90, 93, 95)
That’s 10 observations total.

Why your expected values differ from R’s output

Your expected Q1=68 and Q3=90 likely come from splitting the dataset into four equal parts after finding the median:

  • The first half (values 1-5: 55,64,68,74,78) has a median of 68 (your Q1)
  • The second half (values 6-10:80,84,90,93,95) has a median of 90 (your Q3)

This is a valid method, but it’s not what R uses by default.

R’s default quartile method (Hyndman-Fan Type 7)

R’s summary() function relies on the quantile() function with type=7 (the default setting). Here’s how it calculates the quartiles:
For a given quantile p (0.25 for Q1, 0.75 for Q3), it computes the position i using the formula:
i = (n - 1)*p + 1
where n is the number of observations.

Let’s apply this to your data:

  1. Calculating Q1 (p=0.25):
    i = (10-1)*0.25 + 1 = 3.25
    This position means we take the 3rd value (68) plus 25% of the difference between the 4th value (74) and the 3rd value:
    68 + 0.25*(74-68) = 68 + 1.5 = 69.5

  2. Calculating Q3 (p=0.75):
    i = (10-1)*0.75 + 1 = 7.75
    This position takes the 7th value (84) plus 75% of the difference between the 8th value (90) and the 7th value:
    84 + 0.75*(90-84) = 84 + 4.5 = 88.5

The median calculation matches your expectation because both methods average the 5th and 6th values: (78+80)/2 =79.

Get your expected values in R

If you want to use the method that gives Q1=68 and Q3=90, you can specify type=2 in the quantile() function:

quantile(data, c(0.25, 0.5, 0.75), type=2)

This will return:

25%  50%  75% 
 68   79   90 

Key takeaway

There’s no single "right" way to calculate quartiles—different statistical methods and software use different conventions. R’s default is just one of several valid approaches.

内容的提问来源于stack exchange,提问作者Shahbaz Hussain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:22:51