[R] adding tables

Eik Vettorazzi E.Vettorazzi at uke.de
Tue Dec 10 20:10:04 CET 2013


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



More information about the R-help mailing list