RINNER Heinrich
2010-Jan-26 16:26 UTC
[R] tapply and more than one function, with different arguments
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.
Peter Ehlers
2010-Jan-26 18:00 UTC
[R] tapply and more than one function, with different arguments
Try replacing 'max' with 'mean' and see what you get. Then have a look at ?max and see what max() does with extra arguments. I'm not sure it's relevant, but it might be useful to check what Hmisc::summarize does. -Peter Ehlers RINNER Heinrich 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 at 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. > >-- Peter Ehlers University of Calgary
Dennis Murphy
2010-Jan-26 18:44 UTC
[R] tapply and more than one function, with different arguments
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.RINNER@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]]