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
Hi Karl, same to me. Much of the times when coding I think, 'damn it, I have seen that before, but where...' ... and so the following is from scratch, not from memory. fun<-function(x,...){ mthd<-list(...) lapply(mthd,function(m) do.call(m,list(x))) } fun(3.14, mode, typeof, class) there is no error-catching for non-existing functions, no naming of results and so on, but it may be a start. hth. Am 02.02.2011 14:59, schrieb Karl Ove Hufthammer:> 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) >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790
Eik Vettorazzi wrote:> ... and so the following is from scratch, not from memory. > > fun<-function(x,...){ > mthd<-list(...) > lapply(mthd,function(m) do.call(m,list(x))) > } > fun(3.14, mode, typeof, class) > > there is no error-catching for non-existing functions, no naming of > results and so on, but it may be a start.Thanks for the suggestion. I took the liberty of adding function names and support for multiple objects. Here?s the resulting function (?msum? is short for ?multiple summaries?, or something like that ?): msum = function(x,...){ fun.names = sapply(lapply(substitute(list(...)), deparse)[-1], paste, collapse="") mthd<-list(...) if(!is.list(x)) x = list(x) res = t(sapply(x, function(y) sapply(mthd, function(m) do.call(m, list(y)) ))) colnames(res) = fun.names rownames(res) = names(x) res } It works for simple objects: $ msum( rnorm(100, 2, 3), mean, median, sd, mad) mean median sd mad [1,] 2.380686 2.410399 3.073316 3.034474 For multiple objects: $ x = list(3, 3L, 3.14, factor(3), "3") $ msum(x, mode, typeof, class) mode typeof class [1,] "numeric" "double" "numeric" [2,] "numeric" "integer" "integer" [3,] "numeric" "double" "numeric" [4,] "numeric" "integer" "factor" [5,] "character" "character" "character" And it adds row names if they are specified: $ x = list(norm=rnorm(100), exp=rexp(100), gamma=rgamma(100, 3, 5)) $ msum(x, mean, median ) mean median norm -0.1539711 -0.1048998 exp 0.9726821 0.7661752 gamma 0.5556737 0.4678005 Hope someone else will find it useful too. -- Karl Ove Hufthammer
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