Can anyone recommend a good way to add tables? Ideally I would like t1 <- table(x1) t2 <- table(x2) t1+t2 It t1 and t2 have the same levels this works fine, but I need something that will work even if they differ, e.g., > t1 1 2 4 5 2 1 1 1 > t2 <- table(c(10, 11, 12, 13)) > t1+t2 # apparently does simple vector addition 1 2 4 5 3 2 2 2 whereas I want 1 2 4 5 10 11 12 13 2 1 1 1 1 1 1 1
Answering myself... On Mon, 2013-12-09 at 15:59 -0800, Ross Boylan wrote:> Can anyone recommend a good way to add tables?For count data, which were my main concern, it looks as if tabulate with nbins will work. I'm not sure how this works with a cross-classifying factor, which I will also need. It's possible the factor may have missing values in some tabulations as well. For the one dimensional case > t1 <- tabulate(c(1, 2, 2, 4), nbins=5) > t2 <- tabulate(c(2, 3, 5), nbins=5) > t1 [1] 1 2 0 1 0 > t2 [1] 0 1 1 0 1 > t1+t2 [1] 1 3 1 1 1> Ideally I would like > t1 <- table(x1) > t2 <- table(x2) > t1+t2 > > It t1 and t2 have the same levels this works fine, but I need something > that will work even if they differ, e.g., > > t1 > > 1 2 4 5 > 2 1 1 1 > > t2 <- table(c(10, 11, 12, 13)) > > t1+t2 # apparently does simple vector addition > > 1 2 4 5 > 3 2 2 2 > whereas I want > 1 2 4 5 10 11 12 13 > 2 1 1 1 1 1 1 1 >
On Dec 9, 2013, at 3:59 PM, Ross Boylan wrote:> Can anyone recommend a good way to add tables? > Ideally I would like > t1 <- table(x1) > t2 <- table(x2) > t1+t2 > > It t1 and t2 have the same levels this works fine, but I need something > that will work even if they differ, e.g., >> t1 > > 1 2 4 5 > 2 1 1 1 >> t2 <- table(c(10, 11, 12, 13)) >> t1+t2 # apparently does simple vector addition > > 1 2 4 5 > 3 2 2 2 > whereas I want > 1 2 4 5 10 11 12 13 > 2 1 1 1 1 1 1 1R contingency tables are really matrices, so the `cbind` matrix-method appears to be what you want: cbind(t1,t2)>David Winsemius Alameda, CA, USA
HI, May be this helps: t2 <- table(c(10,11,12,13)) ?t1 <-table(c(1,1,2,4,5)) t <- c(t1,t2) tapply(t,sort(as.numeric(names(t))),sum) A.K. On Monday, December 9, 2013 7:01 PM, Ross Boylan <ross at biostat.ucsf.edu> wrote: Can anyone recommend a good way to add tables? Ideally I would like t1 <- table(x1) t2 <- table(x2) t1+t2 It t1 and t2 have the same levels this works fine, but I need something that will work even if they differ, e.g.,> t11 2 4 5 2 1 1 1> t2 <- table(c(10, 11, 12, 13)) > t1+t2? # apparently does simple vector addition1 2 4 5 3 2 2 2 whereas I want 1 2 4 5 10 11 12 13 2 1 1 1? 1? 1? 1? 1 ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
How about this: t2 <- table(c(10,11,12,13)) t1 <-table(c(1,1,2,4,5)) t12<-(rbind(as.data.frame(t1),as.data.frame(t2))) xtabs(Freq~.,t12) it works for "overlapping" tables t2 <- table(c(10,11,12,13,1,2)) t1 <-table(c(1,1,2,4,5,11,12)) t12<-(rbind(as.data.frame(t1),as.data.frame(t2))) xtabs(Freq~.,t12) and it will work for two-dimensional tables as well: t2 <- table(c(10,11,12,13),letters[rep(1:2,each=2)]) t1 <-table(c(1,1,2,4,5),letters[rep(c(1,3),length.out=5)]) t12a<-(rbind(as.data.frame(t1),as.data.frame(t2))) xtabs(Freq~.,t12a) cheers. Am 10.12.2013 00:59, schrieb Ross Boylan:> Can anyone recommend a good way to add tables? > Ideally I would like > t1 <- table(x1) > t2 <- table(x2) > t1+t2 > > It t1 and t2 have the same levels this works fine, but I need something > that will work even if they differ, e.g., > > t1 > > 1 2 4 5 > 2 1 1 1 > > t2 <- table(c(10, 11, 12, 13)) > > t1+t2 # apparently does simple vector addition > > 1 2 4 5 > 3 2 2 2 > whereas I want > 1 2 4 5 10 11 12 13 > 2 1 1 1 1 1 1 1 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Eik Vettorazzi Department of Medical Biometry and Epidemiology University Medical Center Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Besuchen Sie uns auf: www.uke.de _____________________________________________________________________ Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Christian Gerloff (Vertreter des Vorsitzenden), Prof. Dr. Dr. Uwe Koch-Gromus, Joachim Pr?l?, Rainer Schoppik _____________________________________________________________________ SAVE PAPER - THINK BEFORE PRINTING