[R] adding tables

David Winsemius dwinsemius at comcast.net
Tue Dec 10 02:01:07 CET 2013


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  1

R contingency tables are really matrices, so the `cbind` matrix-method appears to be what you want: 

cbind(t1,t2)

> 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list