A typical example in which diagonals can be helpful is Social Network Analysis. For example, if we use matrices to represent friendship perceptions between individuals, then we need a dyadic matrix.

# generate a dyadic matrix for 3 individuals
m <- matrix(sample(0:1, 9, replace=TRUE), nrow=3, ncol=3)
m
##      [,1] [,2] [,3]
## [1,]    1    0    1
## [2,]    0    0    1
## [3,]    0    1    0

Let says that we want to look at second-order connections (i.e. friends of friends). If we now want to represent the data from both time period in a single object, we need a 4-dimensional array. Higher-order arrays are hard to visualise, another way of doing this is by representing two dimensions along each of the two edges of a matrix. We can do this using the Knonecker Product (denoted ⊗), which we can call in R using the alias %x%.

M <- m %x% m
M
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]    1    0    1    0    0    0    1    0    1
##  [2,]    0    0    1    0    0    0    0    0    1
##  [3,]    0    1    0    0    0    0    0    1    0
##  [4,]    0    0    0    0    0    0    1    0    1
##  [5,]    0    0    0    0    0    0    0    0    1
##  [6,]    0    0    0    0    0    0    0    1    0
##  [7,]    0    0    0    1    0    1    0    0    0
##  [8,]    0    0    0    0    0    1    0    0    0
##  [9,]    0    0    0    0    1    0    0    0    0

Feelings of friendship towards oneself aren’t always particularly insightful. We can now use the diagonals library to eliminate those.

# load the library
library(diagonals)

# remove the elements along the diagonal of width 2
minus_block_matrix(M, size=2)
## Warning in minus_block_matrix(M, size = 2): Matrix dimensions are not a
## multiple of size, problems will occur in the bottom right (South-East) of
## the output matrix.
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]    0    0    1    0    0    0    1    0    1
##  [2,]    0    0    1    0    0    0    0    0    1
##  [3,]    0    1    0    0    0    0    0    1    0
##  [4,]    0    0    0    0    0    0    1    0    1
##  [5,]    0    0    0    0    0    0    0    0    1
##  [6,]    0    0    0    0    0    0    0    1    0
##  [7,]    0    0    0    1    0    1    0    0    0
##  [8,]    0    0    0    0    0    1    0    0    0
##  [9,]    0    0    0    0    1    0    0    0    0