user2806363
user2806363

Reputation: 2593

How to convert data frame to igraph graph object ?

I have graph in data frame formate:

  V          V2       Weight
1 RNF14           V1DR 0.4055584
2 RNF14 DKFZp586J0119 0.1407795
3 RNF14         SMAD4 0.3571942
4 RNF14        UBE2D4 0.5070112
5 RNF14        EIF2B5 0.1407795

I would like to compute all weighted shortest path distance between all pairs of nodes. I tried to use

distances(graph, v = V(graph), to = V(graph), mode = c("all", "out",
  "in"), weights = NULL, algorithm = c("automatic", "unweighted",
  "dijkstra", "bellman-ford", "johnson"))

from igraph package, but it need the graph input object. I was wondering how can I convert data frame to graph object while keeping the weights of the edges?

I've found following function that converts data frame to graph object, but I'm not sure how to keep the edge weights on it

graph_from_data_frame(relations, directed=TRUE, vertices=actors)

Upvotes: 2

Views: 2735

Answers (1)

katie lu
katie lu

Reputation: 499

g = graph.data.frame(df[,c('V','V2')])
E(g)$weight = df$Weight

The order of links from the initial graph should be the same as in the data frame. You can further validate by selection several links from both data frame and from the graph to check if weight matches:

df[c(5:10),]

It should match:

E(g)[5:10]
E(g)$weight[5:10]

Upvotes: 0

Related Questions