计算R中数据帧中每列的百分位数
我有一个由3个分类列和40个数值列组成的数据集。我想分别计算40个数字列中每个列的第90个百分位数 以该数据框为例:计算R中数据帧中每列的百分位数,r,dataframe,percentile,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.
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_百分位数=分位数(df[,3:5],probs=0.90)
我想要的结果是:
# Column 90thPercentile
# price1 17
# price2 8.4
# price3 10.1
考虑到我有40个专栏,一个接一个地这样做是不现实的。谢谢你的帮助 使用
dplyr
和tidyr
:
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
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