获取按另一列分组的多列的频率计数

Getting frequency counts for multipe columns grouped BY another column

我正在做一份调查问卷,分析将基于地理区域(我数据中的一列 table)。

R 中,我试图找出一种方法来按地理区域 (KPG) 汇总我的整个问卷。因此,每个地理区域作为一行,每个可能的问题答案(A001、A0002 等)作为一列(包括 0 值)。

table(dummyframe$KPG, dummyframe$A001)
      1 2 3 4 5
  111 0 1 1 0 0
  112 1 1 0 0 0
  113 4 0 1 0 0
  114 0 3 1 1 0
  115 0 0 1 2 1
  116 1 0 0 0 0
xtabs(~KPG+A001,dummyframe)
 A001
KPG   1 2 3 4 5
  111 0 1 1 0 0
  112 1 1 0 0 0
  113 4 0 1 0 0
  114 0 3 1 1 0
  115 0 0 1 2 1
  116 1 0 0 0 0

双向 return 所需格式的频率计数和 return 问题 1table 的格式

我希望能够通过这样添加来为我的问卷中的许多列执行此操作:

table(dummyframe$KPG, df$A001+A002)

但这会导致根据问题 1 评估区域,然后根据问题 1 评估问题 2,而我希望按区域评估问题 1,按区域评估问题 2,但这些问题不会相互评估。

我想在一个步骤中将 table 函数分别应用于我的数据框的每一列,然后将答案绑定在一起,这样我的 table 就是所有按区域划分的答案。我尝试使用 aggregate

aggregate(.~KPG, dummyframe, count)
KPG    A001       A002       A003       A004
1 111    2, 3       4, 5       2, 3       1, 3
2 112    1, 2       3, 5       3, 4       1, 2
3 113    1, 3 1, 2, 3, 4    1, 3, 4    1, 2, 4
4 114 2, 3, 4 1, 2, 3, 4    1, 3, 4 0, 1, 2, 4
5 115 3, 4, 5    2, 4, 5 0, 2, 3, 4       0, 3
6 116       1          1          2          1
 A005
1    0, 4
2       4
3 0, 2, 3
4    1, 4
5 0, 1, 4
6       2

这会导致在给出答案 1、3 和 5 时每个网格单元格都填充有 c(1,3,5) 值,正如您可以假设的那样,这非常无益。

有循环的想法吗? lapply?轻拍?

更新:添加数据

structure(list(KPG = c(111L, 111L, 112L, 112L, 113L, 113L, 113L, 
113L, 113L, 114L, 114L, 114L, 114L, 114L, 115L, 115L, 115L, 115L, 
116L), A001 = c(2L, 3L, 1L, 2L, 1L, 1L, 3L, 1L, 1L, 2L, 2L, 4L, 
2L, 3L, 3L, 4L, 5L, 4L, 1L), A002 = c(4L, 5L, 5L, 3L, 2L, 1L, 
3L, 4L, 2L, 3L, 2L, 4L, 4L, 1L, 4L, 5L, 5L, 2L, 1L), A003 = c(3L, 
2L, 3L, 4L, 3L, 4L, 1L, 4L, 4L, 4L, 1L, 3L, 3L, 4L, 2L, 4L, 0L, 
3L, 2L), A004 = c(1L, 3L, 1L, 2L, 2L, 1L, 1L, 1L, 4L, 4L, 2L, 
1L, NA, 0L, 3L, 0L, 3L, 0L, 1L), A005 = c(0L, 4L, 4L, 4L, 0L, 
0L, 3L, 3L, 2L, 1L, 1L, 4L, 1L, 4L, 4L, 0L, 1L, 1L, 2L)), .Names =      c("KPG", 
"A001", "A002", "A003", "A004", "A005"), row.names = c(NA, 19L
), class = "data.frame")

更新:预期输出

    A001      A002      A003      A004      A005
    1 2 3 4 5 1 2 3 4 5 0 1 2 3 4 0 1 2 3 4 0 1 2
111 0 1 1 0 0 0 0 0 1 1 0 0 1 1 0 0 1 0 1 0 1 0 0
112 1 1 0 0 0 0 0 1 0 1 0 0 0 1 1 0 1 1 0 0 0 0 0
113 4 0 1 0 0 1 2 1 1 0 0 1 0 1 3 0 3 1 0 1 2 0 1
114 0 3 1 1 0 1 1 1 2 0 0 1 0 2 2 1 1 1 0 1 0 3 0
115 0 0 1 2 1 0 1 0 1 2 1 0 1 1 1 2 0 0 2 0 1 2 0
116 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1

do.call("cbind", lapply(names(dummyframe[-1]), function(x) { temp <- as.data.frame.matrix(table(dummyframe[["KPG"]], dummyframe[[x]])); setNames(temp, paste0(x, names(temp))) }))

--> 按照建议给你预期的输出,但合并了问题和答案编号(可以很容易地在 Excel 中格式化)

我们可以通过使用 lapply 然后 cbind 一起将结果

扩展到多列
do.call("cbind", lapply(df[-1], function(x) table(df$KPG, x)))


#    1 2 3 4 5 1 2 3 4 5 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
#111 0 1 1 0 0 0 0 0 1 1 0 0 1 1 0 0 1 0 1 0 1 0 0 0 1
#112 1 1 0 0 0 0 0 1 0 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 2
#113 4 0 1 0 0 1 2 1 1 0 0 1 0 1 3 0 3 1 0 1 2 0 1 2 0
#114 0 3 1 1 0 1 1 1 2 0 0 1 0 2 2 1 1 1 0 1 0 3 0 0 2
#115 0 0 1 2 1 0 1 0 1 2 1 0 1 1 1 2 0 0 2 0 1 2 0 0 1
#116 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0