[R] How do I access with the name of a (passed) function

Ajay Shah ajayshah at mayin.org
Sat Oct 17 13:26:53 CEST 2009


How would I do something like this:

f <- function(x, g) {
  s <- as.character(g)               # THIS DOES NOT WORK
  sprintf("The %s of x is %.0f\n", s, g(x))
}

f(c(2,3,4), "median")
f(c(2,3,4), "mean")

and get the results 

   "The median of x is 3"
   "The mean of x is 3"

-- 
Ajay Shah                                      http://www.mayin.org/ajayshah  
ajayshah at mayin.org                             http://ajayshahblog.blogspot.com
<*(:-? - wizard who doesn't know the answer.




More information about the R-help mailing list