[R] dynamic argument names and values as variables inside a loop

From: Christopher Marcum <cmarcum_at_uci.edu>
Date: Fri, 08 Apr 2011 11:28:47 -0700


Greetings All!

After much trial and error, and an exhaustive search of the archives, I'm writing to solicit help with a problem.

I want to dynamically modify variable names and values as function arguments inside a loop.

I have a canned function from a package that takes ellipsis (...) arguments of the form varb=vval where varb is a name and vval is either numeric or character. I want to create a dynamic version of this function for batch processing that allows the varb and vval to be variable. For instance, in this toy example:

a.func<-function(...){
  does.something(...)
 }

#I need to pass varb as a character

my.func<-function(dat,varb){
  dat.col<-dat[,varb]
  vval<-unique(dat.col)
  for(i in 1:length(vval){
  outpm<-a.func(eval(args))
  }
 return(outpm)
}

#Intuitively, the following should work (but does not):

my.func<-function(dat,varb){
  dat.col<-dat[,varb]
  vval<-unique(dat.col)
  for(i in 1:length(vval){

args<-attr(parse(text=paste(varb,"=","\"",vval[i],"\"",sep="")),"wholeSrcref")   outpm<-a.func(eval(args))
  }
return(outpm)
}

Any help is much appreciated, and I thank you for your time.

Here is a link to a similar problem that another useR recently posted: https://stat.ethz.ch/pipermail/r-help/2011-January/264719.html

Best,
Christopher Marcum
Doctoral Candidate
Sociology Department
University of California, Irvine

        [[alternative HTML version deleted]]



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 Fri 08 Apr 2011 - 19:24:07 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 08 Apr 2011 - 19:30:27 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