[R] Applying multiple functions to one object

Den d.kazakiewicz at gmail.com
Fri Feb 4 16:44:32 CET 2011


'Aggregate multiple functions into a single function. Combine multiple
functions to a single function returning a named vector of outputs'

This is a short description of each() function from plyr package

Here is an example from help

each(min, max)(1:10)

Hope this helps 
Regards
Denis




У Пят, 04/02/2011 у 08:37 -0600, jctoll піша:
> On Wed, Feb 2, 2011 at 7:59 AM, Karl Ove Hufthammer <karl at huftis.org> wrote:
> > Dear list members,
> >
> > I recall seeing a convenience function for applying multiple functions to
> > one object (i.e., almost the opposite of 'mapply’) somewhere.
> > Example: If the function was named ’fun’ the output of
> >
> >  fun(3.14, mode, typeof, class)
> >
> > would be identical to the output of
> >
> >  c(mode(3.14), typeof(3.14), class(3.14))
> >
> > Is my memory failing me, or does such a function already exists in a
> > package? Of course, it’s not difficult to define a summary function and
> > apply this to the object, but writing, for example,
> >
> > fun(x, mean, median, sd, mad)
> >
> > to quickly show the relevant information is much more *convient*.
> >
> >
> > It would be even nicer with a function that could also handle vectors and
> > lists of values, and output the result as data frames or matrices. Example:
> >
> > x = c("foo", "bar", "foobar")
> > fun(x, nchar, function(st) substr(st, 1 ,2) )
> >
> > y = list(3, 3L, 3.14, factor(3))
> > fun(x, mode, typeof, class)
> >
> > --
> > Karl Ove Hufthammer
> >
> > ______________________________________________
> > 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.
> >
> 
> Karl,
> 
> Perhaps you're thinking of the Reduce function? There's an example
> from the help page that you might be able to adapt to your purpose.
> 
> ## Iterative function application:
> Funcall <- function(f, ...) f(...)
> ## Compute log(exp(acos(cos(0))
> Reduce(Funcall, list(log, exp, acos, cos), 0, right = TRUE)
> 
> HTH,
> 
> James
> 
> ______________________________________________
> 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.



More information about the R-help mailing list