How to sum counts across tables that may contain partially different categories in R?

StackOverflow https://stackoverflow.com/questions/17030647

  •  31-05-2022
  •  | 
  •  

Question

How do I merge (add) contingency tables:

> (t1 <- table(c("a","b","b","c")))

a b c 
1 2 1 
> (t2 <- table(c("c","d","d","a")))

a c d 
1 1 2 

I want this:

a b c d
2 2 2 2
Was it helpful?

Solution

You can do it using split and sapply

> T <- c(t1, t2)
>  sapply(split(T, names(T)), sum)
a b c d 
2 2 2 2 

Or directly using tapply as pointed out by @Arun

> tapply(T, names(T), sum)
a b c d 
2 2 2 2 

OTHER TIPS

Here is what I was able to come up with:

> (t1 <- table(c("a","b","b","c")))

a b c 
1 2 1 
> (t2 <- table(c("c","d","d","a")))

a c d 
1 1 2 
> (n <- sort(union(names(t1),names(t2))))
[1] "a" "b" "c" "d"
> (t1 <- t1[n])

   a    b    c <NA> 
   1    2    1   NA 
> names(t1) <- n
> t1
 a  b  c  d 
 1  2  1 NA 
> t1[is.na(t1)] <- 0
> t1
a b c d 
1 2 1 0 
> t2 <- t2[n]
> names(t2) <- n
> t2
 a  b  c  d 
 1 NA  1  2 
> t2[is.na(t2)] <- 0
> t2
a b c d 
1 0 1 2 
> t1+t2
a b c d 
2 2 2 2 

I think there must be a better way...

This works:

library(plyr)

colSums(rbind.fill(data.frame(t(unclass(t1))), data.frame(t(unclass(t2)))),
        na.rm = T)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top