Christopher Marcum
2011-Apr-08 18:28 UTC
[R] dynamic argument names and values as variables inside a loop
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]]
Reasonably Related Threads
- iterative loop with user input?
- String manipulation, insert delim
- factor manipulation: edgelist to a matrix?
- incomplete results from as.character.srcref() in some cases involving quote()
- incomplete results from as.character.srcref() in some cases involving quote()
