在 R 编程中创建数据集的分位数 – quantile()函数
R 语言中的quantile()
函数用于在概率[0, 1] 的数据集中创建样本分位数。比如第一个分位数在 0.25[25%],第二个在 0.50[50%],第三个在 0.75[75%]。
Syntax: quantile(x)
Parameters:
x: Data set
示例 1:
# R program to create
# quantiles of a data set
# Create a data frame
d <- data.frame( name = c("Abhi", "Bhavesh", "Chaman", "Dimri"),
age = c(7, 5, 9, 16),
ht = c(46, NA, NA, 69),
school = c("yes", "yes", "no", "no") )
# Calling quantile() Function
quantile(d$age)
输出:
0% 25% 50% 75% 100%
5.00 6.50 8.00 10.75 16.00
示例 2:
# R program to create
# quantiles of a data set
# Calling pre-defined data set
BOD
# Calling quantile() Function
quantile(BOD$demand)
quantile(BOD$Time)
输出:
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
0% 25% 50% 75% 100%
8.300 11.625 15.800 18.250 19.800
0% 25% 50% 75% 100%
1.00 2.25 3.50 4.75 7.00