Hi R-users,

Since R.2.11 aggregate can now deal with non-scalar functions, which is 
very useful to me.

However, I have a question about how best to process the output.

test <- data.frame(a = rep(c("g1", "g2"), each = 50), b = runif(100))

res <- aggregate(test$b, list(group = test$a), function(x) quantile(x, 
probs = c(0.05, 0.95)))

 > res
   group       x.5%      x.95%
1    g1 0.00899229 0.91327509
2    g2 0.01863110 0.86187829

Looks like exactly what I want, but:

dim(res)
[1] 2 2

 > res[, 2]
              5%       95%
[1,] 0.00899229 0.91327509
[2,] 0.01863110 0.86187829

 > sapply(res, class)
    group        x
"factor" "matrix"

I first thought the output contained 3 columns, but it does not (and I 
*think*
the help says as much).

So if I want 3 columns and all outputs accessible, I do:

res <- data.frame(subset(res, select = -x), res$x)
 > dim(res)
[1] 2 3

And that works fine.

Is this the best way of obtaining a data frame with the 3 columns?

I know in this example I could have used tapply, but the actual data
consists of several variables.

Thanks.

David
-- 


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to