Re: [R] Passing function to tapply as a string

From: Izmirlian, Grant (NIH/NCI) [E] <izmirlig_at_mail.nih.gov>
Date: Sat, 08 Mar 2008 23:16:21 -0500

>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.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sun 09 Mar 2008 - 04:19:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 09 Mar 2008 - 07:30:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive