简体   繁体   中英

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

I'm attempting to collapse my dataset into the mean and 75th percentiles of a variable, but I can't seem to find the correct way to state that I want the 75th percentile. Code below.

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

We can use 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)

Using a reproducible example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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