Create a matrix of connectivity in R by counting total membership

Suppose I have a dataset in R that indicates the affiliation of countries in international organizations (the original dataset can be found here: IGO_stateunit_v2.3.zip ).

Here's an example of a basic data structure:

cntr <- c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J')
UNO <- c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1)
APEC <- c(0, 0, 0, 0, 1, 1, 1, 0, 0, 0)
ASEAN <- c(0, 0, 0, 0, 1, 1, 0, 0, 0, 0)
data <- data.frame(cntr, UNO, APEC, ASEAN)

      

So the data looks like this: 1 = Organization membership:

cntr UNO APEC ASEAN
A   0    0     0
B   1    0     0
C   1    0     0
D   1    0     0
E   1    1     1
F   1    1     1
G   1    1     0
H   1    0     0
I   1    0     0
J   1    0     0

      

What I would like to create with this data in R is a matrix that takes into account the number of members shared by the two countries. The result should look like this:

cntr A B C D E F G H I J
   A 0 0 0 0 0 0 0 0 0 0
   B 0 0 1 1 1 1 1 1 1 1
   C 0 1 0 1 1 1 1 1 1 1
   D 0 1 1 0 1 1 1 1 1 1
   E 0 1 1 1 0 3 2 1 1 1
   F 0 1 1 1 3 0 2 1 1 1
   G 0 1 1 1 2 2 0 1 1 1
   H 0 1 1 1 1 1 1 0 1 1
   I 0 1 1 1 1 1 1 1 0 1
   J 0 1 1 1 1 1 1 1 1 0

      

Does anyone know how to calculate the connectivity matrix? Help would be greatly appreciated!

+3


source to share


1 answer


Your data:

d <- structure(list(cntr = structure(1:10, .Label = c("A", "B", "C", 
"D", "E", "F", "G", "H", "I", "J"), class = "factor"), UNO = c(0L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), APEC = c(0L, 0L, 0L, 0L, 
1L, 1L, 1L, 0L, 0L, 0L), ASEAN = c(0L, 0L, 0L, 0L, 1L, 1L, 0L, 
0L, 0L, 0L)), .Names = c("cntr", "UNO", "APEC", "ASEAN"), class = "data.frame", row.names = c(NA, 
-10L))

      

Decision:



  m <- as.matrix(d[,-1])
  m2 <- m %*% t(m)
  # Alternatively, m2 <- tcrossprod(m) can be used with the same result (thanks to @akrun for pointing that out)!
  diag(m2) <- 0
  dimnames(m2) <- list(LETTERS[1:10],LETTERS[1:10])
  m2
  #   A B C D E F G H I J
  # A 0 0 0 0 0 0 0 0 0 0
  # B 0 0 1 1 1 1 1 1 1 1
  # C 0 1 0 1 1 1 1 1 1 1
  # D 0 1 1 0 1 1 1 1 1 1
  # E 0 1 1 1 0 3 2 1 1 1
  # F 0 1 1 1 3 0 2 1 1 1
  # G 0 1 1 1 2 2 0 1 1 1
  # H 0 1 1 1 1 1 1 0 1 1
  # I 0 1 1 1 1 1 1 1 0 1
  # J 0 1 1 1 1 1 1 1 1 0

      

EDIT: A slightly more compact solution:

  rownames(d) <- d$cntr
  m <- tcrossprod(as.matrix(d[,-1]))
  diag(m) <- 0
  m

      

+6


source







All Articles