[R] multiple versions of function

Michael Weylandt michael.weylandt at gmail.com
Thu Jan 10 12:30:33 CET 2013



On Jan 9, 2013, at 9:00 PM, ivo welch <ivo.welch at gmail.com> wrote:

> mea culpa.
> 
> f <- function(...) {
>  ## parse out the arguments and then do something with them
> }
> 
> ## all of these should result in the same actions
> f(2,3)  ## interprets a to be first and b to be second
> f(a=2,b=3)
> f(b=3,a=2)

These will all be the same automatically for functions with the signature f(a, b, ...) [grammatical not variadic ellipsis there] -- basic call matching, nothing fancy. 

> f(data.frame(a=2,b=3))
> f(data.frame(b=3,a=1))
> 

Perhaps test if your first arg is a df and, if so, loop over it row-wise building the function calls with do.call() -- something like: 

# Untested
f <- function(a, b){
    if(is.data.frame(a)) return(lapply(seq_len(NROW(a)), function(n) do.call(f, a[n,]))
    ## regular function code here
}

You should probably also fiddle with Recall() to make the recursive structure a little more robust. 

Though it seems that generic functions make more sense here.

Michael Weylandt


> 
> 
> On Tue, Jan 8, 2013 at 8:00 AM, David Winsemius <dwinsemius at comcast.net> wrote:
>> 
>> On Jan 7, 2013, at 6:58 PM, ivo welch wrote:
>> 
>>> hi david---can you give just a little more of an example?  the
>>> function should work with call by order, call by name, and data frame
>>> whose columns are the names.  /iaw
>> 
>> It is I who should be expecting you to provide an example.
>> 
>> -- David.
> 
> ______________________________________________
> 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.




More information about the R-help mailing list