Shafique, M. (UNU-MERIT)
2011-Apr-09 21:34 UTC
[R] Converting edgelist to symmetric matrix
Hi, I have network data in the form of a couple of edgelists containing weights in the format "x,y,weight" whereby x represents row header and y represents column header. All edgelists are based on links among 634 nodes and I need to convert them into a 634*634 weighted matrix. I searched for online help using possible keywords I know, but I could not find a clue how to do this in R. Any help will be appreciated. Best regards, Shafique [[alternative HTML version deleted]]
----------------------------------------> Date: Sat, 9 Apr 2011 14:34:28 -0700 > From: kmshafique at yahoo.com > To: r-help at r-project.org > Subject: [R] Converting edgelist to symmetric matrix > > Hi, > I have network data in the form of a couple of edgelists containing weights in > the format "x,y,weight" whereby x represents row header and y represents column > header. All edgelists are based on links among 634 nodes and I need to convert > them into a 634*634 weighted matrix. > >not find> a clue how to do this in R. Any help will be appreciated.I'm trying to do something related and found ?read.graph will format=ncol do what you need? This apparently creates a graph object that likely has capacilities you need.? Again, I haven't actually used any of this just found while trying to solve a different problem. 'It is a simple text file with one edge per line. An edge is defined by two symbolic vertex names separated by whitespace. (The symbolic vertex names themselves cannot contain whitespace. They might followed by an optional number, this will be the weight of the edge; the number can be negative and can be in scientific notation. If there is no weight specified to an edge it is assumed to be zero. '> > Best regards, > Shafique >
Mike Marchywka
2011-Apr-11 12:41 UTC
[R] Converting edgelist to symmetric matrix/ plotting sparse network with lots of nodes
> Date: Sat, 9 Apr 2011 14:34:28 -0700 > From: kmshafique at yahoo.com > To: r-help at r-project.org > Subject: [R] Converting edgelist to symmetric matrix > > Hi, > I have network data in the form of a couple of edgelists containing weights in > the format "x,y,weight" whereby x represents row header and y represents column > header. All edgelists are based on links among 634 nodes and I need to convert > them into a 634*634 weighted matrix. > > > I searched for online help using possible keywords I know, but I could not find > a clue how to do this in R. Any help will be appreciated.I'd replied earlier suggesting the ncol format, I'd like to follow up on that as I have tried with some success but maybe someone else can comment on alternatives and suggest ideas for plotting. I have a set of nodes or states specified by two parameters ( these are isotopes specified by proton and mass connected by decay paths with probability of that path being its weight). This seems to almost work for your needs( note that I have taken out a lot of extraneous stuff and may have dropped somethimng important LOL, also setup for Rgraphviz is not simple on 'dohs as i had to manually edit env variable etc) , library("Rgraphviz") library("QuACN") nxg<-read.graph("ncol.txt",format="ncol") nn<-igraph.to.graphNEL(nxg) aasd<-adjacencyMatrix(nn)> str((aasd))?num [1:2561, 1:2561] 0 100 95.8 2.7 0 0 0 0 0 0 ... ?- attr(*, "dimnames")=List of 2 ? ..$ : chr [1:2561] "17_10" "17_9" "16_9" "15_7" ... ? ..$ : chr [1:2561] "17_10" "17_9" "16_9" "15_7" ... $ head ncol.txt 17_10 17_9 100 17_10 16_9 95.8 17_10 15_7 2.7 18_10 18_9 100 19_10 19_9 100 23_10 23_11 100 243_100 239_98 100 245_100 241_98 100 246_100 242_98 92 246_100 246_99 1 However, for my needs plotting has been a big problem. I apparently have 2561 isotopes ( none of this has been validated yet LOL) that are sparely connected by a few decay modes ( presumably acyclic directed graph but DAG in searches didn't help much ). Any thoughts on which R classes to try to visualize this or even what I should be thinking about artistically? This is largely just a way to learn R for some other things I want to do for analyzing data on wireless devices but I am curious about this result too. Some of the things I did try are below, library("Rgraphviz") library("QuACN") nxg<-read.graph("ncol.txt",format="ncol") foo<-adjacencyMatrix(nxg) ?graphNEL ?NELgraph df<-data.frame(nxg) plot.igraph(nxg,layout=layout.svd) rglplot.igraph(nxg,layout=layout.svd) rglplot.igraph(nxg,layout=layout.svd) rglplot.igraph(nxg) tkplot.igraph(nxg) library("tcltk") tkplot.igraph(nxg) tkplot(nxg) dx<-decompose.graph(nxg) nn<-igraph.to.graphNEL(nxg) igraph.plotting(nxg) library("sna") gplot(nxg) dx<-get.adjacency(nxg) gplot(dx) gplot3d(dx) plot(nxg) library("ElectroGraph") eg<-electrograph(nxg) eg<-electrograph(aasd) plot(eg) Thanks.> > Best regards, > Shafique >
Hi Shafique, If your edgelist is in the form of a text file (elist.csv) that looks like this: from, to, weight vertex1, vertex2, 3 vertex2, vertex3, 2.3 vertex4, vertex1, 1.2 ... you can convert that to a matrix using library(igraph) edge.list <- read.csv(elist.csv,header=TRUE) g <- graph.data.frame(edge.list, directed=FALSE) get.adjacency(g, type="both", attr="weight") More options for exporting the adjacency matrix are here: http://igraph.sourceforge.net/doc/R/conversion.html If you give more details about your data format you might get more specific help. HTH, Gary On Apr 11, 2011, at 6:00 AM, r-help-request@r-project.org wrote:> > > > > > ---------------------------------------- >> Date: Sat, 9 Apr 2011 14:34:28 -0700 >> From: kmshafique@yahoo.com >> To: r-help@r-project.org >> Subject: [R] Converting edgelist to symmetric matrix >> >> Hi, >> I have network data in the form of a couple of edgelists containing weights in >> the format "x,y,weight" whereby x represents row header and y represents column >> header. All edgelists are based on links among 634 nodes and I need to convert >> them into a 634*634 weighted matrix. >> >> > > not find >> a clue how to do this in R. Any help will be appreciated. > > I'm trying to do something related and found > > ?read.graph > > will format=ncol do what you need? This apparently creates a graph object that likely > has capacilities you need.? Again, I haven't actually used any of this > just found while trying to solve a different problem. > > 'It is a simple text file > with one edge per line. An edge is defined by two symbolic vertex > names separated by whitespace. (The symbolic vertex names > themselves cannot contain whitespace. They might followed by an > optional number, this will be the weight of the edge; the number > can be negative and can be in scientific notation. If there is no > weight specified to an edge it is assumed to be zero. > ' > >> >> Best regards, >> Shafique >> > >-- Gary Weissman http://www.babelgraph.org/ gary@babelgraph.org [[alternative HTML version deleted]]