[R] How to write an S4 method for sum or a Summary generic

Douglas Bates bates at stat.wisc.edu
Mon Apr 19 18:37:43 CEST 2004


"Swinton, Jonathan" <Jonathan.Swinton at astrazeneca.com> writes:

> If I have a class Foo, then i can write an S3 method for sum for it:
> 
> >setClass("Foo",representation(a="integer"));aFoo=new("Foo",a=c(1:3,NA))
> >sum.Foo <- function(x,na.rm){print(x);print(na.rm);sum(x at a,na.rm=na.rm)}
> >sum(aFoo)
> 
> But how do I write an S4 method for this? All my attempts to do so have
> foundered. For example
> >setMethod("sum",signature("Foo","logical"),

> function(x,na.rm){print(x);print(na.rm);sum(x at a,na.rm=na.rm)}
> creates a method which seems to despatch on na.rm="Foo":

There is no x argument in the generic so you can't dispatch on it.

> sum
function (..., na.rm = FALSE) 
.Internal(sum(..., na.rm = na.rm))




More information about the R-help mailing list