[R] How to do aggregate operations with non-scalar functions
Itay Furman
itayf at u.washington.edu
Thu Apr 7 07:38:11 CEST 2005
On Wed, 6 Apr 2005 james.holtman at convergys.com wrote:
> Here is a method that I use in this situation. I work with the indices of
> the rows so that copies are not made and it is fast.
>
> Result <- lapply(split(seq(nrow(df)), df$a), function(.a){ # partition on
> the first variable
> lapply(split(.a, df$z[.a]), function(.z){ # partition on the second
> variable -- notice the subsetting
> c(quantile(df$r[.z]), ...anything else you want to compute)
> })
> })
> Result <- do.call('rbind', Result) # create a matrix - now you have your
> results
>
> Jim
Jim,
Thank you for your reply. For some reason, when I try your
proposed solution I get:
Error in sort(unique.default(x), na.last = TRUE) :
`x' must be atomic
Eventually, I used the solution proposed by Gabor G in this
thread. One advantage of his solution is that it is easier to
scale up I believe; for example in the case you have 3 factors
that together subset the data frame.
Regards,
Itay
----------------------------------------------------------------
itayf at u.washington.edu / +1 (206) 543 9040 / U of Washington
More information about the R-help
mailing list