[R] Trying to build up functions with its names by means of lapply

Julio Sergio juliosergio at gmail.com
Wed Jun 5 04:53:11 CEST 2013


I want to generate specific gamma distribution functions, given fixed 
parameters. 
This is I have k, and theta, say

   k <- 32.2549       # shape
   theta <- 26.32809  # scale


   # I have an auxiliary function that produces funcions according to 
   # a given character (this is to have either dgamma, pgamma or qgamma)
   # for the specific parameters given above:

   faux <- function(c) {
     function (x) get(paste0(c,"gamma"))(x,k,scale=theta)
   }

   # So I can have, for instance, dgamma, and pgamma with

   dff <- faux("d")
   pff <- faux("p")

   dff(1000)
   ## [1] 0.001433138

   pff(1000)
   ## [1] 0.844305

Now, if I try to produce both functions in one shot with lapply, the thing 
doesn't work, see

   ffs <- lapply(c("d", "p"), faux)

   ffs[[1]](1000)
   ## [1] 0.844305 

   ffs[[2]](1000)
   ## [1] 0.844305 
 
The two produced functions are the very same and correspond to pgamma!!
Maybe I'm missing something. Do you have any idea?

Thanks,

  -Sergio.



More information about the R-help mailing list