I have a square matrix with information on the co-voting behavior between individuals (15x15 in toy example below). The rows and columns of the matrix are arranged according to the groups the individuals belong to (A, B or C). The entries indicate whether or not two individuals voted the same way 50% of the time (possible entries: 1, 0, NaN).
I need to calculate the rate/fraction of co-voting within and between groups. The resulting matrix in the toy example should be a 3x3 matrix with A, B, C on the rows and columns and values ranging from 0 to 1. How can I do this using for loops?
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
CodePudding user response:
If your matrix is called m
, you could do
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
Created on 2022-06-02 by the reprex package (v2.0.1)
Data taken from question in reproducible format
m <- structure(c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L,
1L, 0L, 1L, 1L, 1L, 1L, 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