Hi: Using the plyr package, we can get the result as follows:
> library(plyr) > my.fun <- function(x, mult) mult*sum(x) > dat <- data.frame(x = 1:4, grp = c("a","a","b","b")) > ddply(dat, .(grp), summarize, max = max(x), myfun = my.fun(x, 10)) grp max myfun 1 a 2 30 2 b 4 70 HTH, Dennis On Tue, Jan 26, 2010 at 8:26 AM, RINNER Heinrich < heinrich.rin...@tirol.gv.at> wrote: > Dear R-users, > > I am working with R version 2.10.1. > > Say I have is a simple function like this: > > > my.fun <- function(x, mult) mult*sum(x) > > Now, I want to apply this function along with some other (say 'max') to a > simple data.frame, like: > > > dat <- data.frame(x = 1:4, grp = c("a","a","b","b")) > > Ideally, the result would look something like this (if mult = 10): > max my.fun > a 2 30 > b 4 70 > > I have tried it that way: > > apply.more.functions <- function(dat, FUN = c("max", "my.fun"), ...) { > res <- NULL > for(f in FUN) res[[f]] <- tapply(dat$x, dat$grp, FUN = f, ...) > data.frame(res) > } > > # let's test it: > > apply.more.functions(dat, FUN = c("max", "min")) > max min > a 2 1 > b 4 3 > # perfect! > > # now, with an additional argument: > > apply.more.functions(dat, FUN = c("max", "my.fun"), mult = 10) > max my.fun > a 10 30 > b 10 70 > # uhuh! > Apparently, 'mult' has been used in the calculation of 'max' as well. > How can I modify apply.more.functions in order to avoid this? > > Your advice would be appreciated; > Kind regards > Heinrich. > > ______________________________________________ > 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. > [[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.