Hello,
 
I am trying to wrap some code that I repeatedly use into a function for efficiency. The following is a toy example simply to illustrate the problem. 
 
foobar.fun<-function(data,idvar,dv){
id.list<-unique(idvar)
result<-numeric(0)
     for (i in id.list){
     tmp1<-subset(data, idvar == i)
     result[i]<-mean(get("tmp1")[[dv]])
     }
return(result)
}
 
The issue is that when the variable 'dv' is replaced by the name of the actual variable in the dataframe the function works as expected. However, when 'dv' is used the function does not identify this as a variable, even though it is one of the function arguments and the function fails.
 
How can function arguments be passed to a loop in such cases? 
 
Thank you,
Harold

	[[alternative HTML version deleted]]


