是否有办法在R中的SummaryBy函数中获得第75个百分位数?

问题描述 投票:1回答:1

我正在尝试将我的数据集折叠到变量的均值和第75个百分位数,但是我似乎找不到正确的方法来声明我想要第75个百分位数。下面的代码。

six_month_agg <- summaryBy(pd ~ industry + region + date, FUN=c(mean, 0.75), data=six_month_pd)
r statistics collapse
1个回答
1
投票

我们可以使用quantile

library(doBy)
summaryBy(pd ~ industry + region + date, FUN= 
           function(x) c(Mean = mean(x), Quantile = quantile(x, probs = 0.75)), data=six_month_pd)

使用可复制的示例

data(warpbreaks)
out <- summaryBy(breaks ~ wool + tension, warpbreaks, FUN=function(x)
     c(Mean = mean(x), Quantile = quantile(x, probs = .75)))

str(out)
#'data.frame':  6 obs. of  4 variables:
# $ wool               : Factor w/ 2 levels "A","B": 1 1 1 2 2 2
# $ tension            : Factor w/ 3 levels "L","M","H": 1 2 3 1 2 3
# $ breaks.Mean        : num  44.6 24 24.6 28.2 28.8 ...
# $ breaks.Quantile.75%: num  54 30 28 31 39 21
© www.soinside.com 2019 - 2024. All rights reserved.