[R] Calculating percentiles from Grouped Data (gapply)

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed Mar 3 22:06:42 CET 2004



Patrick Hausmann wrote:
> 
> Dear R-list,
> 
> I try to calculate the 10- and 90-Percentile from grouped data.
> Using 'lappy' it works fine but I have no success with 'gapply':
> 
> df <- data.frame(test = runif(100))
> df <- df[order(df[,1]),]
> z <- rep(1:10, each = 10)
> lapply(split(df, z), function(x) quantile(x, probs=c(10,90)/100))
> 
> library(nlme)
> gapply(as.data.frame(df), which=1,
>           function(x) quantile(x, probs=c(10,90)/100), group=z)
> #  Error in sort(x, partial = unique(c(lo, hi))) :
> #    `x' must be atomic


For use with quantile(), you have to make x in your anonymous function a
vector as in:

  gapply(as.data.frame(df), which=1,
      function(x) quantile(x[[1]], probs=c(10,90)/100), group=z)


Uwe Ligges

> Thanks for any help
> Patrick
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list