flo
flo

Reputation: 154

Get vertex ids from igraph::shortest_path output - R

I need to read out the vertex names of shortest path sequences in R using the igraph package.

library(igraph)

links = dplyr::tribble(~from, ~to, ~weight,
               1,5,4,
               3,4,5,
               7,1,1,
               3,7,3,
               4,3,2,
               6,5,7,
               2,8,3,
               5,2,9,
               7,6,5)

net = graph_from_data_frame(links, directed = TRUE)

plot(net)

sp = shortest_paths(net, from = "3", to = "8", mode = "out", weights = get.edge.attribute(net, "weight"))

sp$vpath

Plot of the directed network

Now what I get back is the sequence: 3 7 1 5 2 8 as names for a vector with internal ids (2 3 1 7 6 8)

How do I get a simple unnamed integer vector of the vertex names as I defined them in the tribble-function and as they are shown in the plot as well.

I guess it's a very simple problem but nevertheless thank you for helping me!

Upvotes: 1

Views: 291

Answers (1)

flo
flo

Reputation: 154

The answer is:

names(sp$vpath[[1]])

Thank you "user20650"

Upvotes: 3

Related Questions