frank
frank

Reputation: 3608

find distance along a path igraph r

I have a data frame with the respective weights in a graph

set.seed(123)
df = data.frame(a=LETTERS[1:5],b=LETTERS[3:7],w=rnorm(5))
g <- graph_from_data_frame(df)

I would like to know the distance/sum of weights, from a specific path I want. is there a function like this:

path.length('H','L','N')

I can find the shortest path between 2 points, but not for my specific path. Is there a function that calculates this?

Note that my real data is 600 nodes and 900 edges, and my node path is ~90 nodes/edges long

Upvotes: 3

Views: 739

Answers (1)

Tam&#225;s
Tam&#225;s

Reputation: 48061

sum(E(g, path=c("H", "L", "N"))$w) should do the trick. E(g, path=something) selects the edges along the path specified by the vertex names, $w retrieves the weights of these edges (more precisely, the edge attribute named w for all the selected edges), and then sum takes the sum.

Upvotes: 3

Related Questions