[R] How to do aggregate operations with non-scalar functions
Itay Furman
itayf at u.washington.edu
Thu Apr 7 07:31:05 CEST 2005
On Wed, 6 Apr 2005, Rich FitzJohn wrote:
[snip]
> ## This does the hard work of calculating the statistics over your
> ## combinations, and over the values in `p'
> y <- lapply(p, function(y)
> tapply(df$r, list(a=a, x=x), quantile, probs=y))
>
Rich, thank you for your reply. Gabor G has proposed a different
solution that seem to me to be easier to maintain and scale up.
Please see my follow up to his reply.
Your solution introduced to me some R functions I was not
familiar with: expand.grid(), colSums(), and names(). Thanks for
that, too.
> ## Then, we need to work out what combinations of a & x are possible:
> ## these are the header columns. aggregate() does this in a much more
> ## complicated way, which may handle more difficult cases than this
> ## (e.g. if there are lots of missing values points, or something).
> vars <- expand.grid(dimnames(y[[1]]))
In Gabor G's solution this is magically done (I think!) by
do.call().
Thanks,
Itay
----------------------------------------------------------------
itayf at u.washington.edu / +1 (206) 543 9040 / U of Washington
More information about the R-help
mailing list