简体   繁体   English

计算R中数据帧中每列的百分位数

[英]Calculate percentile for every column in a data frame in R

I have a data set of 3 categorial columns and 40 columns with numerical values. 我有一个包含3个分类列和40个数值的数据集。 I want to calculate the 90th percentile for each of the 40 numerical columns separetly. 我想计算出40个数字列中每个列的第90个百分位数。

Take this data frame as a reproducible example: 将此数据框作为可重现的示例:

fruit = c("apple","orange","banana","berry") #1st col
ID = c(123,3453,4563,3235) #2nd col
price1 = c(3,5,10,20) #3rd col
price2 = c(5,7,9,2) #4th col
price3 = c(4,1,11,8) #5th col

df = data.frame(fruit,ID,price1,price2,price3) #combine into a dataframe

I want to do something like: calc_percentile = quantile(df[,3:5], probs = 0.90) 我想做类似的事情: calc_percentile = quantile(df[,3:5], probs = 0.90)

The output I'm looking for would be: 我正在寻找的输出将是:

# Column  90thPercentile
# price1  17
# price2  8.4
# price3  10.1

Doing this one by one is not practical given that I have 40 columns. 鉴于我有40列,这样做是不切实际的。 Your help is appreciated! 非常感谢您的帮助!

stack(lapply(df[3:5], quantile, prob = 0.9, names = FALSE))
#  values    ind
#1   17.0 price1
#2    8.4 price2
#3   10.1 price3

Using dplyr and tidyr : 使用dplyrtidyr

df %>%
 summarise_at(3:5, ~ quantile(., probs = 0.9)) %>%
 gather("Column", "90thPercentile")

  Column 90thPercentile
1 price1           17.0
2 price2            8.4
3 price3           10.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM