计算数据集列的百分位数

Dim*_*nis 31 statistics r percentile

一个快速的,最亲爱的R大师:

我正在做一项任务,在本练习中,我被要求从infert数据集中获取基本统计数据(它是内置的),特别是其中一列infert$age.

对于不熟悉数据集的人:

> table_ages     # Which is just subset(infert, select=c("age"));
    age
1    26
2    42
3    39
4    34
5    35
6    36
7    23
8    32
9    21
10   28
11   29
...
246  35
247  29
248  23
Run Code Online (Sandbox Code Playgroud)

我必须找到列的中值,方差,偏度,标准偏差都可以,直到我被要求找到列"百分位数".

到目前为止,我还没有找到任何东西,也许我从希腊语中错误地翻译了它,这是作业的语言.这是"ποσοστημόρια",谷歌翻译指出英文术语是"百分位数".

找到那些"百分位数"的任何教程或想法infert$age

Rom*_*rik 50

如果您订购了一个向量x,并找到了向量中间值,那么您只需找到一个中位数或第50个百分点.任何百分比都适用相同的逻辑.这是两个例子.

x <- rnorm(100)
quantile(x, probs = c(0, 0.25, 0.5, 0.75, 1)) # quartile
quantile(x, probs = seq(0, 1, by= 0.1)) # decile
Run Code Online (Sandbox Code Playgroud)


ran*_*ndy 26

quantile()函数将完成您可能想要的大部分功能,但由于问题含糊不清,我将提供一个替代答案,它可以做一些稍微不同的事情quantile().

ecdf(infert$age)(infert$age)
Run Code Online (Sandbox Code Playgroud)

将生成一个长度相同的向量,infert$age使其比例infert$age低于每个观察值.您可以阅读ecdf文档,但基本思路是ecdf()为您提供一个返回经验累积分布的函数.因此ecdf(X)(Y),在Y点的X的累积分布的值.如果你想知道低于30的概率(因此样本中的百分位数是30),你可以说

ecdf(infert$age)(30)
Run Code Online (Sandbox Code Playgroud)

这种方法与使用该quantile()函数之间的主要区别在于,quantile()要求您输入概率以获得级别,这需要您输入级别来获取概率.


Gor*_*rka 12

使用 {dplyr}:

library(dplyr)

# percentiles
infert %>% 
  mutate(PCT = ntile(age, 100))

# quartiles
infert %>% 
  mutate(PCT = ntile(age, 4))

# deciles
infert %>% 
  mutate(PCT = ntile(age, 10))
Run Code Online (Sandbox Code Playgroud)


jlh*_*ard 6

table_ages <- subset(infert, select=c("age"))
summary(table_ages)
#            age       
#  Min.   :21.00  
#  1st Qu.:28.00  
#  Median :31.00  
#  Mean   :31.50  
#  3rd Qu.:35.25  
#  Max.   :44.00  
Run Code Online (Sandbox Code Playgroud)

这大概就是他们要找的。summary(...)应用于数字返回数据的最小值、最大值、平均值、中位数以及第 25 个和第 75 个百分位数。

注意

summary(infert$age)
#    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#   21.00   28.00   31.00   31.50   35.25   44.00 
Run Code Online (Sandbox Code Playgroud)

数字相同,但格式不同。这是因为table_ages是一列(年龄)的数据框,而infert$age是数字向量。尝试打字summary(infert)