[R] Function Arguments

From: Doran, Harold <HDoran_at_air.org>
Date: Sun 27 Mar 2005 - 06:43:12 EST


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]]



R-help@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 Received on Sun Mar 27 06:48:27 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:56 EST