From: jim holtman <jholtman_at_gmail.com>

Date: Thu, 10 May 2007 22:43:52 -0400

*>
*

*> examplefun(fun=minus, vars = vars.in)
*

[1] -333 -333 -333

*>
*

Date: Thu, 10 May 2007 22:43:52 -0400

Try this:

> vars.in = data.frame(x='sample1',y='sample2', stringsAsFactors=FALSE)

*> sample1 <- c(111,222,333)
**> sample2 <- c(444,555,666)
**> minus <- function(x,y) {get(x)-get(y)}
**>
**>
**> examplefun <- function(fun, vars=vars.in, ...) {
*

+ # convert 'vars' to a list and call the function + do.call(fun, as.list(vars)) + }

[1] -333 -333 -333

On 5/10/07, new ruser <newruser_at_yahoo.com> wrote:

> Thanks, but I need to clarify.

*>
**> Sometimes f"FUN" may take only 1 argument, other times it may take several
**> arguments.
**>
**> How can I write the code so that the appropriate arguments are
**> "automatically" passed to fun, using the column names in vars.in as the
**> "argument names" and the contents of row1 of vars.in as the arguments?
**>
**> jim holtman <jholtman_at_gmail.com> wrote:
**> You weren't passing in any 'x' and 'y' arguments to the 'fun'.
**>
**> Try this:
**>
**> vars.in = data.frame(x='sample1',y='sample2')
**>
**>
**> examplefun <- function(fun, vars=vars.in, ...) {
**>
**> for(v in 1: ncol(vars) ) {
**> assign( names(vars)[v] , vars[1,v] , env=.GlobalEnv)
**> }
**> fun(x=as.character(vars[1,1]), y=as.character(vars[1,2]))
**> }
**>
**> examplefun(fun=minus, vars = vars.in)
**>
**>
**> On 5/10/07, new ruser wrote:
**> > I have searched the r-help files but have not been able to find an answer
**> to this question. I apologize if this questions has been asked previously.
**> >
**> > (Please excuse the ludicrousness of this example, as I have simplified my
**> task for the purposes of this help inquiry. Please trust me that something
**> like this will in fact be useful what I am trying to accomplish. I am using
**> R 2.4.1 in Windows XP.)
**> >
**> > I have created two functions:
**> >
**> > 1. minus <- function(x,y) {get(x)-get(y)}
**> >
**> > a. note: x and y are of type character and represent the names of
**> numerical objects)
**> >
**> >
**> > 2. examplefun <- function(fun, vars=vars.in, ...) {
**> >
**> > for(v in 1: ncol(vars) ) {
**> > assign( names(vars)[v] , vars[1,v] , env=.GlobalEnv)
**> > }
**> >
**> > fun(...) }
**> >
**> > a. FUN = another function (e.g. minus() that takes as its inputs variables
**> of type chatrcater
**> > b. vars = (a data.frame with the names of variables that will be passed to
**> "FUN"
**> >
**> > My problem: various inputs for "FUN" will require different arguments.
**> These arguments will be contained in the column names of "vars". How do I
**> pass these arguments to FUN()?
**> >
**> >
**> > My example and my attempt:
**> >
**> > what I am trying to do, successfully accomplished without a function:
**> > x='sample1'
**> > y='sample2'
**> >
**> > sample1=c(222,333,444)
**> > sample2=c(100,200,300)
**> >
**> > minus <- function(x,y) {get(x)-get(y)}
**> > minus(x,y)
**> >
**> > #########################################################
**> >
**> > #My so-far failing attempt to write a function to do the same thing:
**> >
**> > vars.in = data.frame(x='sample1',y='sample2')
**> >
**> >
**> > examplefun <- function(fun, vars=vars.in, ...) {
**> >
**> > for(v in 1: ncol(vars) ) {
**> > assign( names(vars)[v] , vars[1,v] , env=.GlobalEnv)
**> > }
**> >
**> > fun(...) }
**> >
**> > examplefun(fun=minus, vars = vars.in)
**> >
**> >
**> > ---------------------------------
**> > Now that's room service! Choose from over 150,000 hotels
**> >
**> > [[alternative HTML version deleted]]
**> >
**> > ______________________________________________
**> > R-help_at_stat.math.ethz.ch 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.
**> >
**>
**>
**> --
**> Jim Holtman
**> Cincinnati, OH
**> +1 513 646 9390
**>
**> What is the problem you are trying to solve?
**>
**>
**>
**> ________________________________
**> Expecting? Get great news right away with email Auto-Check.
**> Try the Yahoo! Mail Beta.
**>
**>
*

-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? ______________________________________________ R-help_at_stat.math.ethz.ch 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 Fri 11 May 2007 - 02:48:52 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 Fri 11 May 2007 - 03:31:57 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.
*