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.
Yes, tapply(rnorm(100), gl(5,20), "max") On 07/03/2008, Yuri Volchik <yuri.volchik at gmail.com> wrote:> > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Richard.Cotton at hsl.gov.uk
2008-Mar-07 13:25 UTC
[R] Passing function to tapply as a string
> Was wondering if it is possible to pass function name as a parameterYes. This isn't exactly what you wanted, but it demonstrates the principle. x = rnorm(5) [1] -0.6510448 0.4591730 1.3225205 1.2314391 -0.0888139 myfun <- function(fname, x) eval(parse(text=paste(fname,"(x)",sep=""))) myfun('max',x) [1] 1.322521 myfun('min',x) [1] -0.6510448 Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
Izmirlian, Grant (NIH/NCI) [E]
2008-Mar-09 04:16 UTC
[R] Passing function to tapply as a string
>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.
Izmirlian, Grant (NIH/NCI) [E]
2008-Mar-09 05:29 UTC
[R] FW: [OT] "normal" (as in "Guassian")
Wouldn't you agree that maybe the word 'normal' has something to do with the concept of 'average' as it follows from a given CLT applying to partial sums, S_n, of a given sequence having identical means, mu, and variances, sig^2, that for finite n, a we have the following weak approximation: S_n/n is approximately, in Distribution, equal to N( mu, sig^2/ n) and by the LIL, the approximation is, for large n, with probability 1, accurate to +/- (2 sig^2 log(log(n)))^0.5/n^0.5 --- John Fox <jfox at mcmaster.ca> wrote:> Dear Doug, > > As I recall, according to Stigler, yes -- he wasn't > the first to > formulate Stigler's law of eponymy (but I don't > recall to whom he > attributed it).Possibly a disgruntles M. de Moivre?> > Regards, > John > > On Mon, 3 Mar 2008 12:17:59 -0600 > "Douglas Bates" <bates at stat.wisc.edu> wrote: > > On Mon, Mar 3, 2008 at 8:25 AM, Duncan Murdoch > <murdoch at stats.uwo.ca> > > wrote: > > > On 3/3/2008 9:10 AM, Rogers, James A [PGRD > Groton] wrote: > > > > As someone of partly French heritage, I would > also ask how this > > > > distribution came to be called "Gaussian". It > seems very unfair > > to de > > > > Moivre, who discovered the distribution at > least half a century > > earlier. > > > > :-) > > > > > > Just an example of Stigler's Law. > > > > Taking this to a whole new level of "off topic", I > wonder if > > Stigler's > > Law is self-referential? That is, should > Stigler's Law more > > correctly > > be attributed to someone else? > > > > > > On Mar 2, 2008, at 7:33 AM, (Ted Harding) > wrote: > > > > > > > >> Hi Folks, > > > >> Apologies to anyone who'd prefer not to see > this query > > > >> on this list; but I'm asking because it is > probably the > > > >> forum where I'm most likely to get a good > answer! > > > >> > > > >> I'm interested in the provenance of the name > "normal > > > >> distribution" (for what I'd really prefer to > call the > > > >> "Gaussian" distribution). > > > >> > > > >> According to Wikipedia, "The name "normal > distribution" > > > >> was coined independently by Charles S. > Peirce, Francis > > > >> Galton and Wilhelm Lexis around 1875." > > > >> > > > >> So be it, if that was the case -- but I > would like to > > > >> know why they chose the name "normal": what > did they > > > >> intend to convey? > > > >> > > > >> As background: I'm reflecting a bit on the > usage in > > > >> statistics of "everyday language" as > techincal terms, > > > >> as in "significantly different". This, for > instance, > > > >> is likely to be misunderstood by the general > publidc > > > >> when they encounter statements in the media. > > > >> > > > >> Likewise, "normally distributed" would > probably be > > > >> interpreted as "distributed in the way one > would > > > >> normally expect" or, perhaps, "there was > nothing > > > >> unusual about the distribution." > > > >> > > > >> Comments welcome! > > > >> With thanks, > > > >> Ted. > > > >> > > > > > > > > > ______________________________________________ > > > > 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. > > > > > > ______________________________________________ > > > 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. > > > > > > > ______________________________________________ > > 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. > > -------------------------------- > John Fox, Professor > Department of Sociology > McMaster University > Hamilton, Ontario, Canada > http://socserv.mcmaster.ca/jfox/ > > ______________________________________________ > 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. >
Also note that there is match.funfn in the gsubfn package. That allows you to also pass functions defined as formulas: e.g. library(gsubfn) f.at.four <- function(f) match.funfn(f)(4) f.at.four(sqrt) # 2 f.at.four("sqrt") # 2 f.at.four(~ x^.5) # 2 - uses function(x) x^.5 See homepage, ?match.funfn and the vignette. On Mon, Mar 10, 2008 at 11:46 AM, Yuri Volchik <yuri.volchik at gmail.com> wrote:> > Thanks, > > match.fun is what i was looking for :-) > > > > > > > Or perhaps: > > myfun <- function(fname, ...)match.fun(fname)(...) > > On 07/03/2008, Richard.Cotton at hsl.gov.uk <Richard.Cotton at hsl.gov.uk> wrote: > > > Was wondering if it is possible to pass function name as a parameter > > > -- > View this message in context: http://www.nabble.com/Passing-function-to-tapply-as-a-string-tp15891151p15950836.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. >