[R] how to convert a table to adjacency matrix used in social network analysis?

Chuck Cleland ccleland at optonline.net
Wed Feb 27 12:05:16 CET 2008


On 2/27/2008 3:13 AM, Samuel wrote:
> Hi Guys,
> 
> Do you any one know how to convert a long format table to an adjacency
> matrix used in sna? The long table looks like
> 
> p1 p2 counts
> a b 100
> a c 200
> a d 100
> b c 80
> b d 90
> b e 100
> c d 100
> c e 40
> d e 60
> 
> and I want to convert it to an adjacency matrix which can be used in sna?
> 
> Any methods will be appreciated!

   The graph package has some nice tools for this.

mydf <- data.frame(p1=c('a','a','a','b','b','b','c','c','d'),
                    p2=c('b','c','d','c','d','e','d','e','e'),
                    counts=c(100,200,100,80,90,100,100,40,60))

library(graph)

myadjM <- ftM2adjM(as.matrix(mydf[,1:2]), W=mydf$counts)

myadjM
   a   b   c   d   e
a 0 100 200 100   0
b 0   0  80  90 100
c 0   0   0 100  40
d 0   0   0   0  60
e 0   0   0   0   0

> btw, besides sna package, is there any better package can be used in social
> network analysis, specially good at plotting?

   For plotting I would look into the Rgraphviz package.  Here is a 
simple diagram of the network:

library(Rgraphviz)

mygraph <- ftM2graphNEL(as.matrix(mydf[,1:2]), W=mydf$counts)

plot(mygraph)

   I'm not sure how to incorporate the weights for each edge into the 
diagram, but maybe that is explained in the documentation for the sna 
and Rgraphviz packages.

> Thanks in advance!
> 
> Regards, 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list