Patrick Hausmann
2004-Mar-03 10:53 UTC
[R] Calculating percentiles from Grouped Data (gapply)
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 Thanks for any help Patrick
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 atomicFor 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