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

北战南征 提交于 2020-01-05 05:25:28

问题


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)


来源:https://stackoverflow.com/questions/17030647/how-to-sum-counts-across-tables-that-may-contain-partially-different-categories

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!