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

时间:2018-08-02 17:24:31

标签: r dataframe percentile

我有3个类别列和40个具有数值的列的数据集。我想分别计算40个数字列的第90个百分位数。

以该数据框为可重现示例:

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

我想做以下事情:calc_percentile = quantile(df[,3:5], probs = 0.90)

我正在寻找的输出将是:

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

考虑到我有40列,一一进行是不切实际的。感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

tf.one_hot

答案 1 :(得分:1)

使用dplyrtidyr

library(dplyr)
library(tidyr)
df[,3:5] %>%
  summarise_all(funs(list(quantile(., probs = 0.9)))) %>%
  gather("Column", "90thPercentile")