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? -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
one approach is the following: x <- as.table(c(cats=3,squid=7,pigs=2,dogs=1,slugs=0)) y <- as.table(c(cats=4,dogs=5,slugs=3,crabs=0)) ######################## out <- merge(as.data.frame(x), as.data.frame(y), by = "Var1", all = TRUE) desired.output <- rowSums(out[c("Freq.x", "Freq.y")], na.rm = TRUE) names(desired.output) <- out$Var1 as.table(desired.output) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Robin Hankin" <r.hankin at noc.soton.ac.uk> To: "RHelp" <r-help at stat.math.ethz.ch> Sent: Monday, June 19, 2006 9:33 AM Subject: [R] combining tables> 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? > > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Try this: both <- c(x,y) as.table(tapply(both, names(both), sum)) 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? > > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >