[R] Need car() and cdr() for '...'

Paul Roebuck roebuck at odin.mdacc.tmc.edu
Tue Nov 9 21:28:42 CET 2004


Needed to redefine function "sum" for my MATLAB package.
There's something similar in Chambers's Green Book (pg 351)
so I modified it as such:

library(methods)
setGeneric("sum", function(x, ..., na.rm = FALSE) {
    if (nDotArgs(...) > 0)
        sum(c(sum(x, na.rm = na.rm),
              sum(..., na.rm = na.rm)))
    else
        standardGeneric("sum")
})
setMethod("sum", "vector", function(x, na.rm) {
    return(base::sum(x, na.rm));
})
setMethod("sum", "matrix", function(x, na.rm) {
    return(apply(x, 2, sum, na.rm));
})
setMethod("sum", "array", function(x, na.rm) {
    stop('Argument "x" must either be a vector or matrix')
})
setMethod("sum", "missing", function() {
    stop('Argument "x" missing')
})

Problem is that R's definition for summary functions
doesn't specify the "x" argument so the generic
won't work. So does someone have LISP-derived car/adr
functions I can use to split the '...' list such that
the generic function could use this instead:

        sum(c(sum(car(...), na.rm = na.rm),
              sum(cdr(...), na.rm = na.rm)))

Of course, better ideas are welcome too.

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)




More information about the R-help mailing list