Skip to content

Reduce: extra args wishlist?

4 messages · Ben Bolker, Brian Ripley, Gabor Grothendieck

#
Is there a reason that Reduce() doesn't take a "..." argument that
would allow arbitrary extra arguments to be passed through to the function?

  Here is a little example of how this would be convenient:

z <- list(
          data.frame(state=c("California"),
                     cases=0),
          data.frame(state=c("California","Massachusetts"),
                     cases=c(1,2)),
          data.frame(state=c("California","Massachusetts","Arizona"),
                     cases=c(1,2,17)))

## read in revised version of Reduce()
Reduce(merge,z,by="state",all=TRUE)

  The change is simple -- just add "..." to every call to the function
in Reduce() --  patch is included below my signature ...

  It's not a big deal -- I could also do
Reduce(function(x,y) { merge(x,y,by="state",all=TRUE) }, z)
but there doesn't seem to be a good reason not to allow it ...

  cheers
    Ben Bolker
3 days later
#
On Sat, 6 Jun 2009, Ben Bolker wrote:

            
I think the idiom of the languages from which this comes would indeed 
to be use an anonymous function, and in R to use ... and no braces, as 
in

Reduce(function(...) merge(..., by="state", all=TRUE), z)

But that's not the (ony) R idiom, so I am happy to add a ... argument 
to Reduce.  Unfortunately the patch below is backwards (you have diffs 
from old to new in 'patch' format), and we do need to patch the 
documentation (which gets a bit ugly as ... has a different meaning 
for another function on the Reduce help page).  I think I have sorted 
these out, and will commit to R-devel shortly.

  
    
#
Prof Brian Ripley wrote:
Hmm.  Thanks.  Sorry about the patch glitch.  (On my Ubuntu system,
patch has a -R flag that would seem to take care of that ...)

  I accepted Kurt's explanation.  I'm curious what the protocol is when
R-core members differ?  (I would have guessed that conservatism would
rule, or the opinion of the original author of the functions (I don't
know who contributed Reduce et al.), but perhaps Kurt doesn't have
strong feelings about this ...

  cheers
    Ben
#
If ... is not available you can get a minor reduction using fn$ in gsubfn.
Any function call prefaced by fn$ allows the use of formulas as functions
(and perl-like interpolation of strings) in the call args (subject to certain
rules that determine which args are interpreted and which not).  The LHS
of the formula specifies the args but if omitted then it uses the free
variables from the RHS in the order encountered:
state cases.x cases.y cases
1    California       0       1     1
2 Massachusetts      NA       2     2
3       Arizona      NA      NA    17

See http://gsubfn.googlecode.com for more.
On Wed, Jun 10, 2009 at 10:06 AM, Ben Bolker<bolker at ufl.edu> wrote: