Reputation: 189
We want to construct an adjacency matrix (value of 1 if A_Number
called B_Number
, 0 if otherwise).
Weight
= number of times the two numbers called.
Input:
B = matrix(c(1,4,1, 2,4,1, 3,2,1, 4,5,1, 5,2,1),
nrow = 5,
ncol = 3,
byrow = TRUE)
colnames(B) <- c("A_NUMBER", "B_NUMBER", "Weight")
# A_NUMBER B_NUMBER Weight
# [1,] 1 4 1
# [2,] 2 4 1
# [3,] 3 2 1
# [4,] 4 5 1
# [5,] 5 2 1
Wanted output:
We tried following code:
p<-as.matrix(dcast(SNA_data, A_NUMBER~B_NUMBER, value.var="W", fill=0))
But this did not gave as an adjacency matrix as the names of the rows were not the same as the names of the columns. Does somebody know how to make a proper adjacency matrix?
Upvotes: 0
Views: 542
Reputation: 18425
xtabs(Weight ~ A_NUMBER + B_NUMBER,B)
will do it. If you want the zero rows/columns as well, just add some dummy rows (Weight=0) to your matrix, covering the full range of values that A_NUMBER and B_NUMBER might take.
Upvotes: 1