From ggrothendieck at gmail.com  Tue Jun 20 13:01:31 2006
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Tue, 20 Jun 2006 07:01:31 -0400
Subject: [Rd] [R] combining tables
In-Reply-To: <5CE7128B-2D15-4CFD-9E68-FE3318782A03@soc.soton.ac.uk>
References: <B469259B-333B-41BF-958C-D95DAB37B303@soc.soton.ac.uk>
	<971536df0606190445he5a6ccyd57c1b6fbed4dafa@mail.gmail.com>
	<5CE7128B-2D15-4CFD-9E68-FE3318782A03@soc.soton.ac.uk>
Message-ID: <971536df0606200401p63a17fddy25fa341059737b03@mail.gmail.com>

On 6/20/06, Robin Hankin <r.hankin at noc.soton.ac.uk> wrote:
> On 19 Jun 2006, at 12:45, Gabor Grothendieck wrote:
> > Try this:
> >
> > both <- c(x,y)
> > as.table(tapply(both, names(both), sum))
>
>
> thanks for this, Gabor.
>
> The class of the objects I am manipulating in my
> package is c("count", "table").
>
> It'd be nice to overload the "+" symbol so that I can add
> two count objects like this with a simple "x+y".  To this end, I
> defined a Ops.count() function that executed Gabor's summation.
>
> But, this broke all sorts of functionality in the package,
> because the ">" relation was not defined in my Ops.count()
> function.
>
> In my case, the only operation that I want to redefine is "+".
> I want to leave all the others unchanged.
>
> What is Best Practice for redefining just one binary operator?
>
>
>
> >
> > On 6/19/06, Robin Hankin <r.hankin at noc.soton.ac.uk> wrote:
> >> Hi
> >>
> >> Suppose I have two tables of counts of different animals and I
> >> wish to pool them so the total of the sum is the sum of the total.
> >>
> >> Toy example follows (the real ones are ~10^3 species and ~10^6
> >> individuals).
> >> x and y are zoos that have merged and I need a combined inventory
> >> including animals that are temporarily absent.
> >>
> >>
> >>  > (x <- as.table(c(cats=3,squid=7,pigs=2,dogs=1,slugs=0)))
> >> cats squid  pigs  dogs slugs
> >>     3     7     2     1     0
> >>  > (y <- as.table(c(cats=4,dogs=5,slugs=3,crabs=0)))
> >> cats  dogs slugs crabs
> >>     4     5     3     0
> >>  > (desired.output <- as.table(c
> >> (cats=7,squid=7,pigs=2,dogs=6,slugs=3,crabs=0)))
> >> cats squid  pigs  dogs slugs crabs
> >>     7     7     2     6     3     0
> >>  >
> >>
> >>
> >>
> >>
> >> Note that we have 7 cats altogether, and the crabs correctly show
> >> as zero counts.
> >>
> >>
> >> How to do this nicely in R?

This will define + but not the others:

"+.count" <- function(x,y) {
	both <- c(x,y)
	as.table(tapply(both, names(both), sum))
}



[Rd] [R] combining tables
Gabor Grothendieck