交叉表 - 需要根据其他列中的某些值聚合多个列

时间:2017-02-21 22:19:29

标签: r crosstab

我的数据如下所示,所有列都包含二进制存在/不存在数据:

POP1   POP2   POP3    T1    T2    T3    T4    T5    T6    T7    T8    T9
 1      1      0       1     1     1     1     0     1     0     0     1
 1      0      1       0     1     1     0     1     1     0     1     1
 1      1      0       1     1     1     1     0     0     1     0     1
 0      0      0       0     1     1     0     1     0     1     1     0
 1      0      1       0     0     1     1     1     0     1     1     0
 0      1      0       0     1     1     1     0     0     0     0     1
 0      1      0       1     1     0     1     0     0     0     0     0
 1      1      1       0     1     0     0     0     1     0     0     0
 0      0      0       0     1     1     1     1     1     0     0     1
 1      0      0       1     0     1     0     1     0     1     1     1
 1      1      0       0     1     0     1     0     0     1     0     0 
 1      0      1       0     1     1     1     0     1     0     1     0
 0      1      0       1     1     1     1     0     0     0     0     0
 1      0      0       0     1     1     0     0     0     0     1     1

POP1:POP3是人口,我需要所有T1的所有1的计数:所有POP1 = 1,POP2 = 1和POP3 = 1的T9。我需要一个像我这样交叉建立数据的表:

         T1    T2    T3    T4    T5    T6    T7    T8    T9
POP1=1    3     9     7     5     3     4     4     5     5
POP2=1    4     7     8     6     2     3     2     0     3
POP3=1    0     3     4     2     2     2     1     3     1

不要检查汇总的计数,它们不一定正确。我已经尝试了很多合成器而没有得到我想要的东西。感谢一些指导。

2 个答案:

答案 0 :(得分:4)

您需要矩阵乘法%*%

t(df[1:3]) %*% as.matrix(df[4:12]) 

     T1 T2 T3 T4 T5 T6 T7 T8 T9
POP1  3  7  7  5  3  4  4  5  5
POP2  4  7  4  6  0  2  2  0  3
POP3  0  3  3  2  2  3  1  3  1

答案 1 :(得分:2)

df = structure(list(POP1 = c(1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 
1L, 1L, 0L, 1L), POP2 = c(1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 
0L, 1L, 0L, 1L, 0L), POP3 = c(0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 
0L, 0L, 0L, 1L, 0L, 0L), T1 = c(1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 
0L, 1L, 0L, 0L, 1L, 0L), T2 = c(1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 
1L, 0L, 1L, 1L, 1L, 1L), T3 = c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 
1L, 1L, 0L, 1L, 1L, 1L), T4 = c(1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 
1L, 0L, 1L, 1L, 1L, 0L), T5 = c(0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 
1L, 1L, 0L, 0L, 0L, 0L), T6 = c(1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L, 0L, 0L, 1L, 0L, 0L), T7 = c(0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 
0L, 1L, 1L, 0L, 0L, 0L), T8 = c(0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 
0L, 1L, 0L, 1L, 0L, 1L), T9 = c(1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 
1L, 1L, 0L, 0L, 0L, 1L)), .Names = c("POP1", "POP2", "POP3", 
"T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8", "T9"), class = "data.frame", 
row.names = c(NA, -14L))

library(reshape2)
df = melt(df, id.vars = colnames(df)[-(1:3)] )

do.call(rbind, lapply(split(df, df$variable), function(x)
                    apply(x[x$value == 1,1:9], 2, function(y) sum(y))))

#     T1 T2 T3 T4 T5 T6 T7 T8 T9
#POP1  3  7  7  5  3  4  4  5  5
#POP2  4  7  4  6  0  2  2  0  3
#POP3  0  3  3  2  2  3  1  3  1