计算组内和组间的共同投票

Calculating co-voting within and between groups

我有一个方阵,其中包含有关个人之间共同投票行为的信息(下面的玩具示例中为 15x15)。矩阵的行和列根据个体所属的组(A、B 或 C)排列。这些条目表示两个人是否有 50% 的时间以相同方式投票(可能的条目:1、0、NaN)。

我需要计算组内和组间的共同投票 rate/fraction。玩具示例中的结果矩阵应该是一个 3x3 矩阵,行和列上有 A、B、C,值范围为 0 到 1。如何使用 for 循环执行此操作?

  A A A A A B B B B B C C C C C
A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
A 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0
A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
A 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
B 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1
B 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1
B 0 0 0 0 0 1 1 1 1 1 1 1 0 1 1
B 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1
B 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1
C 0 0 1 0 0 1 1 1 1 1 1 1 0 0 1
C 1 1 1 1 1 1 0 1 0 0 1 1 1 0 1
C 1 1 1 1 1 1 0 0 0 0 0 1 1 1 0
C 1 1 1 1 1 1 1 1 0 1 0 0 1 1 0
C 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1

如果你的矩阵叫做m,你可以

groups <- unique(colnames(m))

res <- matrix(0, 3, 3, dimnames = list(groups, groups))

for(i in groups) {
  for(j in groups) {
    mat <- m[rownames(m) %in% i, colnames(m) %in% j]
    res[rownames(res) %in% i, colnames(res) %in% j] <- sum(mat) / length(mat)
  }
}

res
#>      A    B    C
#> A 1.00 0.20 0.64
#> B 0.20 0.92 0.68
#> C 0.64 0.68 0.60

reprex package (v2.0.1)

创建于 2022-06-02

以可复制格式从问题中获取的数据

m <- structure(c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L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