r - Building and adjacency matrix -
i wondering if guys can me building adjacency matrix. have data in cvs format this:
paper_id author 2 foster-mcgregor, n. 3 van houte, m. 4 van de meerendonk, a. 5 farla, k. 6 van houte, m. 6 siegel, m. 8 farla, k. 11 farla, k. 11 verspagen, b.
as can see column "paper_id" has repeated value of 11, meaning "farla, k." , "verspagen, b." coauthors of publication. need build square weighted matrix using names of authors, counting times collaborating together.
does following looking for?
# simulate data. d <- data.frame( id=c(2,3,4,5,6,6,8,11,11,12,12), author=c("fn", "vm","va","fk","vm","sm","fk","fk","vb","fk","vb") ) d id author 1 2 fn 2 3 vm 3 4 va 4 5 fk 5 6 vm 6 6 sm 7 8 fk 8 11 fk 9 11 vb 10 12 fk 11 12 vb # create incidence matrix: m <- xtabs(~author+id,d) m id author 2 3 4 5 6 8 11 12 fk 0 0 0 1 0 1 1 1 fn 1 0 0 0 0 0 0 0 sm 0 0 0 0 1 0 0 0 va 0 0 1 0 0 0 0 0 vb 0 0 0 0 0 0 1 1 vm 0 1 0 0 1 0 0 0 # convert adjacency matrix. # tcrossprod "m %*% t(m)" tcrossprod(m) author author fk fn sm va vb vm fk 4 0 0 0 2 0 fn 0 1 0 0 0 0 sm 0 0 1 0 0 1 va 0 0 0 1 0 0 vb 2 0 0 0 2 0 vm 0 0 1 0 0 2
note crossprod()
give incidence matrix id
variable (i.e. t(m) %*% m
).
Comments
Post a Comment