Hello, This is my first attempt to write a script in R. The program below is intended to do some parametric tests on group data. There are subroutines for each type of test. The call to the "parametric.tests", routine sets the argument "testtype" for the test to be used. How can I transfer the calculated values (in "result" below) in each routine to the calling parametric.tests routine? Cem ---------------- ## testtype : 1: Duncan ## 2: Dunnett ## resp: response variable, must be numeric and vector ## group: group id for resp, numeric or character ## alpha: CL 0.05 or 0.01 ## vehicle: Control group name for Dunnett parametric.tests<-function(testtype, resp, group, vehicle, alpha) { if (testtype==1){ ## resp: response variable, must be numeric and vector ## group: group id for resp, numeric or character ## alpha: CL 0.05 or 0.01 duncan.test <- function (resp, group, alpha) { ......... result <- data.frame(label=label, estimate=Estimate, alpha=alpha, lower=Lower, upper=Upper, p.value=pval, significance=sig) return(result) } } else if (testtype==2){ dunnett.test <- function(resp, group, vehicle, alpha) { ..... result <- data.frame(label=label, estimate=Estimate, alpha=alpha, lower=Lower, upper=Upper, p.value=pval, significance=sig) return(result) } } } Cem
It seems all you are doing in the if statements is defining functions. You need to actually "apply" them to some arguments, then you can pass results. i.e. f<- function(x,type,...){ a<- function(...){ 2* x } b<- function(...) { x^2 } if(type==1){ ret<- a(x) } if(type==2){ ret<- b(x) } ret } You may also want to see ?switch or ?local Cheers On Mon, Feb 13, 2012 at 2:59 PM, Cem Girit <girit at comcast.net> wrote:> Hello, > > ? ? ? ?This is my first attempt to write a script in R. The program below > is intended to do some parametric tests on group data. There are subroutines > for each type of test. The call to the "parametric.tests", routine sets the > argument "testtype" for the test to be used. How can I transfer the > calculated values (in "result" below) in each routine to the calling > parametric.tests routine? > > Cem > > ---------------- > > > ## testtype : 1: Duncan > ## ? ? ? ? ? ? ? ? ? ? 2: Dunnett > ## resp: ?response variable, must be numeric and vector > ## group: group id for resp, numeric or character > ## alpha: CL 0.05 or 0.01 > ## vehicle: Control group name for Dunnett > > parametric.tests<-function(testtype, resp, group, vehicle, alpha) > { > if (testtype==1){ > > ## resp: ?response variable, must be numeric and vector > ## group: group id for resp, numeric or character > ## alpha: CL 0.05 or 0.01 > > ?duncan.test <- function (resp, group, alpha) { > > ......... > > result <- data.frame(label=label, estimate=Estimate, alpha=alpha, > lower=Lower, upper=Upper, p.value=pval, significance=sig) > return(result) > } > } > > else if (testtype==2){ > > > dunnett.test <- function(resp, group, vehicle, alpha) > { > > ..... > > ?result <- data.frame(label=label, estimate=Estimate, alpha=alpha, > lower=Lower, upper=Upper, p.value=pval, significance=sig) > ?return(result) > }> }> } > > Cem > > ______________________________________________ > 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.
On 02/14/2012 08:59 AM, Cem Girit wrote:> Hello, > > This is my first attempt to write a script in R. The program below > is intended to do some parametric tests on group data. There are subroutines > for each type of test. The call to the "parametric.tests", routine sets the > argument "testtype" for the test to be used. How can I transfer the > calculated values (in "result" below) in each routine to the calling > parametric.tests routine? >Hi Cem, You may find it easier to use the do.call method and the clean.args function: library(plotrix) parametric.tests<-function(testfun,arglist) { result<-do.call(deparse(substitute(testfun)), clean.args(arglist,testfun)) return(result) } parametric.tests(mean,list(x=1:5,na.rm=TRUE,foo="?")) If the function called ("testfun") is available, and the argument "arglist" contains sufficient arguments for it to run, it will return the value of the function. The reason for using "clean.args" is in case you are passing a fixed list of arguments, all of which may not be appropriate for any of the functions that you want to pass as "testfun". Jim