簡體   English   中英

有沒有辦法在 R 的 SummaryBy 函數中獲得第 75 個百分位數?

[英]Is there a way to get the 75th percentile in the SummaryBy function in R?

我試圖將我的數據集折疊成變量的平均值和第 75 個百分位數,但我似乎無法找到正確的方法來說明我想要第 75 個百分位數。 代碼如下。

six_month_agg <- summaryBy(pd ~ industry + region + date, FUN=c(mean, 0.75), data=six_month_pd)

我們可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM