R语言中quantile函数求分位数(Quantile function in R language)-R语言
R语言中quantile函数求分位数(Quantile function in R language)
1、分位数表示有百分之多少的数值小于该处的数值。
quantile函数默认返回五个数值:
最小值、第一分位数值、 第二分位(中位数)、第三分位数值、最大值。
当数值个数为奇数时最为简单:
举例1:
> a <- c(1, 3, 4, 6, 7, 9, 15)
> a ## 测试向量, 长度为7,奇数
[1] 1 3 4 6 7 9 15
> quantile(a) ## 中位数为6; 第一分位为3和4的平均值,即3.5; 第三分位为7和9的平均值,即8;
0% 25% 50% 75% 100%
1.0 3.5 6.0 8.0 15.0
举例2:
> a <- c(1, 2, 3, 4, 6, 7, 9, 15, 20)
> a ## 测试向量,长度为9, 奇数
[1] 1 2 3 4 6 7 9 15 20
> quantile(a) ## 中位数为6; 第一分位为3; 第二分位为9;
0% 25% 50% 75% 100%
1 3 6 9 20
当数值个数为偶数时,计算较为复杂。
无论是数值个数是奇数还是偶数,其计算公式为:
x%(n – 1) + 1; 得到的数值表示所求的x%的位置及其左右的权重, 采用加权平均的方法计算具体的分位数。
举例:
1. The quantile indicates what percentage of the value is less than the value there.
The quantile function returns five values by default:
Minimum value, first quantile value, second quantile (median), third quantile value, maximum value.
It is the simplest when the number of values is odd:
Example 1:
> a <- c(1, 3, 4, 6, 7, 9, 15)
> a ## 测试向量, 长度为7,奇数
[1] 1 3 4 6 7 9 15
> quantile(a) ## 中位数为6; 第一分位为3和4的平均值,即3.5; 第三分位为7和9的平均值,即8;
0% 25% 50% 75% 100%
1.0 3.5 6.0 8.0 15.0
Example 2:
> a <- c(1, 2, 3, 4, 6, 7, 9, 15, 20)
> a ## 测试向量,长度为9, 奇数
[1] 1 2 3 4 6 7 9 15 20
> quantile(a) ## 中位数为6; 第一分位为3; 第二分位为9;
0% 25% 50% 75% 100%
1 3 6 9 20
When the number of numerical values is even, the calculation is more complex.
Whether the number of values is odd or even, the calculation formula is:
x%(n – 1) + 1; The obtained value represents the position of X% and its weight, and the specific quantile is calculated by weighted average method.
give an example: