[R] Passing function to tapply as a string

Izmirlian, Grant (NIH/NCI) [E] izmirlig at mail.nih.gov
Sun Mar 9 05:16:21 CET 2008


>From what you've provided its nearly impossible to help you. 
I think that I understand enough to provide a solution the problem
that I believe you are posing.

First some comments:
(1) You can't expect that passing a character string to the argument of 
     tapply that expects a function to work
(2) Note that the INDEX argument of tapply must be of class 'factor'
(3) From what you've provided it isn't clear where 'ttime' and 'fmt'
    are coming from, or even that they are of the same length as
    dfd[,param[1]] which is neccesary in order for this to work,
    but I will assume that you have a handle on that. Since they
    first appear within the definition of function 'dd', then 
    you should most likely include them as arguments to 'dd'
    unless you want to gamble on them being defined either in the
    global frame or the frames ancestral to the frame of evaluation.
(4) I'm willing to bet that you've been using the '...' un-named
     arguments to 'dd' in hopes that this would be a kind of magic
     bandaid..., and I'm willing to bet that you really don't need
     it.


param.to.pass<-c(1,'max','h')

dd<-
  function(dfd, param=param.to.pass, ttime, fmt){
    ttime.int <- format(ttime,fmt)
    ttime.fact <- as.factor(ttime.int)
    data.frame(
       param[3] = tapply(dfd[,param[1]],ttime.fact, 
                         FUN=function(x, fn.nm=param[3]){
                               .call. <- match.call()
                               .call.[[1]] <- as.name(fn.nm)
                             }))
  }



-----Original Message-----
From: Yuri Volchik [mailto:yuri.volchik at gmail.com]
Sent: Fri 3/7/2008 6:06 AM
To: r-help at r-project.org
Subject: [R]  Passing function to tapply as a string
 

Hi,

Was wondering if it is possible to pass function name as a parameter, smth
along this line

param.to.pass<-c(1,'max','h')

dd<-function(dfd, param=param.to.pass,...){
  ttime.int <- format(ttime,fmt)
  data.frame(
     param[3] = tapply(dfd[,param[1]],ttime.int,param[3]),
         ...)
   }


I know there is a as.formula expression but not quite sure if there is some
way to accomplish what i need.
Thanks
-- 
View this message in context: http://www.nabble.com/Passing-function-to-tapply-as-a-string-tp15891151p15891151.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list