0
votes

I have a data.frame

df = data.frame(a=LETTERS[1:5],b=LETTERS[3:7],w=rnorm(5))

and am trying yo put it into igraph for network analysis. When I try

g2 = add.edges(graph.empty(5), t(as.character(df[,1:2])), weight=df[,3])

Error in as.igraph.vs(graph, edges) : Invalid vertex name(s)

I would like to be able to work with the vertices as their names, and not a numeric conversion of the factor. Eventually, I would like to do a further shortest_path and alternative_path analysis.

I am new to igraph, and have not been able to find a solution thus far.

1

1 Answers

1
votes

and am trying yo put it into igraph for network analysis

To put it into a graph, you can do

g <- graph_from_data_frame(df)
plot(g, edge.width=abs(E(g)$w)*2, edge.color=c("black", "red")[(E(g)$w<0)+1L])