[R] Combine tables with different dimensions

Bernd Weiss bernd.weiss at uni-koeln.de
Sun May 7 15:41:37 CEST 2006


Suppose, I have two tables t1 and t2 which are constrcted as follows:

x1 <- c(rep(0:2,10))
x2 <- c(rep(0:1,15))

y1 <- c(rep(5:10,5))
y1 <- as.factor(y1)

y2 <- c(rep(c(5,7),15))
y2 <- as.factor(y2)

t1 <- table(x1,y1)
t2 <- table(x2,y2)


> t1
   y1
x1  5 6 7 8 9 10
  0 5 0 0 5 0  0
  1 0 5 0 0 5  0
  2 0 0 5 0 0  5
> t2
   y2
x2   5  7
  0 15  0
  1  0 15

Now, I would like to merge[1] t1 and t2 so that each missing 
categorie in t2 (compared to t1) is set to NA.

     5  6  7  8  9  10
  0  5  0  0  5  0   0
  1  0  5  0  0  5   0
  2  0  0  5  0  0   5
  0 15 NA  0 NA NA  NA
  1  0 NA 15 NA NA  NA

Of course, this is a toy example and I need to combine t1 to t15. 

It is important to keep the rownames as well as the colnames.

Any help is appreciated.

Bernd


[1] If I could via rbind, which, of course, is not possible, 
dim(t1)[2]!=dim(t2)[2].




More information about the R-help mailing list