Reputation: 193
I have an example of a simple network represented as an adjacency matrix.
I am trying to find the degree of each node/vertex (person)
As defined, the degree of a node is the number of connections it has to other nodes. By convention, in a directed (i.e. asymmetric) matrix, the sender of a tie is the row and the target of the tie is the column.
The adjacency matrix (with 1's on the diagonal):
matrix(c(1,0,1,0,1,1,1,0,1,1,1,1,0,0,1,1), ncol = 4)
The result should be:
Bob: 2 Carol: 2 Ted: 3 Alice: 1
I tried to solve this writing a function, but it does not give a correct result (Carol = 1, not 2)
degree_centralty <- function(x) rowSums(x != 0)-1
Am I doing something wrong?
Upvotes: 1
Views: 483
Reputation: 48211
By using rowSums
or colSums
you would count only one kind of connection. What you want is the union of the two and that can be found as follows:
degree_centralty <- function(x) rowSums(x + t(x) != 0) - 1
degree_centralty(A)
# [1] 2 2 3 1
where now x + t(x)
is a symmetric matrix.
Upvotes: 1