[R] multiple versions of function

David Winsemius dwinsemius at comcast.net
Thu Jan 10 02:43:19 CET 2013


On Jan 9, 2013, at 1:00 PM, ivo welch 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)
> f(data.frame(a=2,b=3))
> f(data.frame(b=3,a=1))
> 
In the last two instances you are only passing a single object. I suppose you could construct the argument list with

f <- function( a=NA, ...) { code}

But this works:

 f <- function(a=NA, b=NA) if( !is.list(a) ) {print(a); cat("\n"); print(b) } else{
                                          with(a, {print(a); cat("\n"); print(b)} ) }

There is some concern for using with in functions so maybe you would want access values with 

   a[["a"]] and a[["b"]]

Test output.


> f(2,3)  
[1] 2

[1] 3
> f(a=2,b=3)
[1] 2

[1] 3
> f(b=3,a=2)
[1] 2

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

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

[1] 3

> 


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

David Winsemius
Alameda, CA, USA




More information about the R-help mailing list