[R] Applying multiple functions to one object

Eik Vettorazzi E.Vettorazzi at uke.uni-hamburg.de
Wed Feb 2 15:19:23 CET 2011


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



More information about the R-help mailing list