8

I am looking for an option to apply many functions to one vector. I think it is kind on an inverse apply function where one function is applied to many vectors (or columns).

Is there a way of specifing two or more functions (like mean and max) and apply this on a vector?

jnshsrs
  • 337
  • 4
  • 11
  • 2
    an example could be appreciated but I guess one way is to do e.g. `v <- rnorm(10); sapply(c("mean","median","sd"), function(nf){get(nf)(v)})` – Cath Jun 22 '15 at 11:54
  • This is one solution I was looking for. Unfortunatly I wasn't familiar with the get function. Thanks solving this despite I didn't provides an example. – jnshsrs Jun 22 '15 at 12:02

1 Answers1

10

Similar to @CathG's comment, but without get:

v <- rnorm(10)
funs <- list(mean, median, sd) 
sapply(funs, function(fun, x) fun(x), x = v)

Or with do.call:

sapply(funs, do.call, args = list(v))
Roland
  • 127,288
  • 10
  • 191
  • 288