How to sum counts across tables that may contain p

2019-08-14 08:48发布

问题:

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

回答1:

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 


回答2:

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...



回答3:

This works:

library(plyr)

colSums(rbind.fill(data.frame(t(unclass(t1))), data.frame(t(unclass(t2)))),
        na.rm = T)


标签: r contingency