[R] followup to "pass operator to function"

Carl Witthoft carl at witthoft.com
Tue Dec 7 01:10:02 CET 2010

In case anyone even remembers that thread, there was a little problem w/ 
some of the solutions provided because here and there are unary rather 
than binary operators or functions.  So, I took two of the responses and 
fixed them up:

dc<-function(op='+',...) do.call(op,list(...))


dcc<-function(op=`+`,...) op(...)  #note the backtick

Note that either of these will work so long as you enter the number of 
required arguments for the operator or function you supply.  Turns out 
they also work just fine if you supply a vector (or vectors, as needed).

And just a reminder about syntax:  dc(,7,6) will execute 7+6



More information about the R-help mailing list