I’ve got a problem with the distances between two nodes of my graph in R.
I’ve build a graph like here:
library(graph)
library(RBGL)
names <- c("a", "b", "c")
g <- new("graphNEL")
g <- addNode(names[1],g)
g <- addNode(names[2],g)
g <- addNode(names[3],g)
g <- addEdge(from=names[1],to=names[2],g)
g <- addEdge(from=names[2],to=names[3],g)
dist <- sp.between(g,names[1],names[3])
dist
# OUTPUT
$`a:c`
$`a:c`$length
[1] 2
$`a:c`$path_detail
[1] "a" "b" "c"
$`a:c`$length_detail
$`a:c`$length_detail[[1]]
a--b b--c
1 1
No I can get the length with:
dist$`a:c`$length
[1] 2
But how can i get the lenght without typin in $`a:c`$length I just only want to use variables like for using a loop to calculate the distances..
dist$names[1]:names[3]$length
Since the result is the list of all shortest paths between the two nodes,
you can take the first one with
[[1]], and extract its length.